English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# provides many decision statements that can help control the flow of a 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 conditions.
C# includes the following types of if statements:
if statement
else-if statement
else statement
The if statement contains a boolean condition followed by a single-line or multi-line code block to be executed. At runtime, if the boolean condition evaluates to true, the code block will be executed; otherwise, it will not be executed.
if(condition) { //Code block to be executed when the if condition is true }
int i = 10, j = 20; if (i < j) { } } if (i > j) { else }
i is less than j
In the above example, the boolean condition i < j in the first if statement evaluates to true, so the C# compiler will execute the following code block. The condition i > j in the second if statement evaluates to false, so the compiler will not execute its code block.
The conditional expression must return a boolean value; otherwise, the C# compiler will produce 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 that returns a boolean value in the if statement.
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; }
multiple else if statements can be used after the if statement. It only executes when the calculation result of the if condition is false. Therefore, either the if statement or one of the else if statements can be executed, but not both at the same time.
if(condition1) { //If condition1the code block to be executed when evaluated as true } else if(condition2) { // the code block that is executed when // condition1is calculated as flase // condition2is calculated as true } else if(condition3) { // the code block that is executed when // condition1is calculated as flase // condition2is calculated as false // condition3is calculated as true }
The following example demonstrates the else if statement.
int i = 10, j = 20; if (i == j) { Console.WriteLine("i equals j"); } else if (i > j) { else } else if (i < j) { } }
i is less than j
The else statement can only appear after an if or else if statement and can only appear in an if-The else statement is used once in the else statement. The else statement cannot contain any conditions and will execute when all the calculation results of the preceding if and else if conditions are 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 equals j"); }
i equals j
C supports another if-if in the else statement-else statement. This is called nested if-else statement. Nested if statements make the code more readable.
if(condition1) { if(condition2) { // the code block that is executed when // condition1and condition2is calculated as true } else if(condition3) { if(condition4) { // the code block that is executed when // only if condition1,condition3and condition4The calculation result is true } else if(condition5) { // the code block that is executed when // only if condition1,condition3and condition5The calculation result is true } else { // the code block that is executed when // condition1and condition3evaluated as true // condition4and condition5The calculation result is false } } }
The following example demonstrates nested if else statements.
int i = 10, j = 20; if (i != j) else if (i > j) { } Console.WriteLine("i is less than j"); { else } } Console.WriteLine("i equals j"); }
i is less than j
Use the ternary operator ? : instead of simple if else statements.