This example shows you How to use c# if else statements. If...else statements make it easy check the condition in c# programming.
How to use C# if else statements
The conditional statement if else in c# language helps you check the conditions you provided in the first part of the if statement and make a decision based on that condition.
if (condition)
statement;
else
statement;
For example, we will check the condition to determine if c variable is a-b or b-a
public int Value(int a, int b)
{
int c = 0;
if (a > b)
c = a - b;
else
c = b - a;
return c;
}
If you want to check more than one conditions at the same time, you can use else if statement.
if (condition1)
statement;
else if (condition2)
statement;
else
statement;
For example, you can implement these conditions in the c# program.
string Gender()
{
bool? gender = null;
if (gender == null)
{
return "???";
}
else if (gender == true)
return "Male";
else
return "Female";
}
I hope so, through the c# example above you can understand how to use if...else statements in c# programming.