C# provides many decision-making statements that help the flow of the C# program based on certain logical conditions. Here, you will learn about if, else if, else, and nested if else statements to control the flow based on the conditions.
C# includes the following flavors of if statements:
- if statement
- else-if statement
- else statement
C# if Statement
The if statement contains a boolean condition followed by a single or multi-line code block to be executed. At runtime, if a boolean condition evaluates to true, then the code block will be executed, otherwise not.
if(condition)
int i = 10, j = 20;
if (i < j)
{
Console.WriteLine("i is less than j");
}
if (i > j)
{
Console.WriteLine("i is greater than j");
}
In the above example, a boolean condition in the first if statement i < j
evaluates to true, so the C# compiler will execute the following code block. The second if
statement's condition i > j evaluates to false, so the compiler will not execute its code
block.
The conditional expression must return a boolean value, otherwise C# compiler will give a compile-time error.
int i = 10, j = 20;
if (i + 1)
{
Console.WriteLine("i is less than j");
}
if (i + j)
{
Console.WriteLine("i is greater than j");
}
You can call a function in the if statement that returns a boolean value.
static void Main(string[] args)
{
int i = 10, j = 20;
if (isGreater(i, j))
{
Console.WriteLine("i is less than j");
}
if (isGreater(j, i))
{
Console.WriteLine("j is greater than i");
}
}
static bool isGreater(int i, int j)
{
return i > j;
}
else if Statement
Multiple else if statements can be used after an if statement. It will only be
executed when the if condition evaluates to false. So, either if or one of the
else if statements can be executed, but not both.
if(condition1)else if(condition2)else if(condition3)
The following example demonstrates else if statements.
int i = 10, j = 20;
if (i == j)
{
Console.WriteLine("i is equal to j");
}
else if (i > j)
{
Console.WriteLine("i is greater than j");
}
else if (i < j)
{
Console.WriteLine("i is less than j");
}
else Statement
The else statement can come only after if or else if statement and can
be used only once in the if-else statements. The else statement cannot contain any
condition and will be executed when all the previous if and else if conditions
evaluate to false.
int i = 20, j = 20;
if (i > j)
{
Console.WriteLine("i is greater than j");
}
else if (i < j)
{
Console.WriteLine("i is less than j");
}
else
{
Console.WriteLine("i is equal to j");
}
Nested if Statements
C# supports if else statements inside another if else statements. This are called
nested if else statements. The nested if statements make the code more readable.
if(condition1)
{
if(condition2)
{
// code block to be executed when
// condition1 and condition2 evaluates to true
}
else if(condition3)
{
if(condition4)
{
// code block to be executed when
// only condition1, condition3, and condition4 evaluates to true
}
else if(condition5)
{
// code block to be executed when
// only condition1, condition3, and condition5 evaluates to true
}
else
{
// code block to be executed when
// condition1, and condition3 evaluates to true
// condition4 and condition5 evaluates to false
}
}
}The following example demonstrates the nested if else statements.
int i = 10, j = 20;
if (i != j)
{
if (i < j)
{
Console.WriteLine("i is less than j");
}
else if (i > j)
{
Console.WriteLine("i is greater than j");
}
}
else
{
Console.WriteLine("i is equal to j");
}
Use Ternary operator ?: instead of a simple if else statement.
C# - Ternary Operator ?:
C# includes a decision-making operator ?: which is called the conditional operator or ternary
operator. It is the short form of the if else conditions.
condition ? statement 1 : statement 2
The ternary operator starts with a boolean condition. If this condition evaluates to true then it will
execute the first statement after ?, otherwise the second statement after : will
be executed.
The following example demonstrates the ternary operator.
int x = 20, y = 10;
var result = x > y ? "x is greater than y" : "x is less than y";
Console.WriteLine(result);
Above, a conditional expression x > y returns true, so the first statement after
? will be execute.
The following executes the second statement.
int x = 10, y = 100;
var result = x > y ? "x is greater than y" : "x is less than y";
Console.WriteLine(result);
Thus, a ternary operator is short form of if else statement. The above example can be re-write
using if else condition, as shown below.
int x = 10, y = 100;
if (x > y)
Console.WriteLine("x is greater than y");
else
Console.WriteLine("x is less than y");
Nested Ternary Operator
Nested ternary operators are possible by including a conditional expression as a second statement.
int x = 10, y = 100;
string result = x > y ? "x is greater than y" :
x < y ? "x is less than y" :
x == y ? "x is equal to y" : "No result";
Console.WriteLine(result);
The ternary operator is right-associative. The expression a ? b : c ? d : e is evaluated
as a ? b : (c ? d : e), not as (a ? b : c) ? d : e.
var x = 2, y = 10;
var result = x * 3 > y ? x : y > z? y : z;
Console.WriteLine(result);