Nested if Statement in C# Programming Language
Table of Content:
Nested if Statements
C# alows nested if else statements. The nested 'if' statement makes the code more readable.
Example: Nested if statements
int i = 10; if (i > 0) { if (i <= 100) { Console.WriteLine("i is positive number less than 100"); } else { Console.WriteLine("i is positive number greater than 100"); } }
Output
i is positive number less than 100
The if-else statement can be replaced by ternary operator. Learn about ternary operator in the next section.
Points to Remember :
- if-else statement controls the flow of program based on the evaluation of the boolean expression.
- It should start from the if statement followed by else or else-if statements.
- Only one else statement is allowed in the if-else chain.
- Multiple else-if statements are allowed in a single if-else chain.
- Nested if-else statement is allowed.