C# if, if...else, if...else if and Nested if Statement
In C#, conditional statements allow you to control the flow of program execution based on a condition. The most basic conditional statement is the if
statement. In this article, we'll cover the if
statement and its variations, including the if...else
statement, the if...else if
statement, and nested if
statements.
The if Statement
The if
statement is used to test a condition and execute a block of code if the condition is true. The syntax of the if
statement is as follows:
1if (condition) 2{ 3 // code to execute if the condition is true 4}
Here's an example:
1int x = 5; 2 3if (x > 0) 4{ 5 Console.WriteLine("x is positive."); 6}
In this example, the if
statement checks whether the value of x
is greater than 0. If it is, the code inside the braces is executed, which prints "x is positive." to the console.
The if...else Statement
The if...else
statement is used to test a condition and execute one block of code if the condition is true, and another block of code if the condition is false. The syntax of the if...else
statement is as follows:
1if (condition) 2{ 3 // code to execute if the condition is true 4} 5else 6{ 7 // code to execute if the condition is false 8}
Here's an example:
1int x = -5; 2 3if (x > 0) 4{ 5 Console.WriteLine("x is positive."); 6} 7else 8{ 9 Console.WriteLine("x is not positive."); 10}
In this example, the if
statement checks whether the value of x
is greater than 0. If it is, the code inside the first set of braces is executed, which prints "x is positive." to the console. If it is not, the code inside the second set of braces is executed, which prints "x is not positive." to the console.
The if...else if Statement
The if...else if
statement is used to test multiple conditions. It works like a series of nested if
statements, with each if
statement being tested in order until a condition is true. The syntax of the if...else if
statement is as follows:
1if (condition1) 2{ 3 // code to execute if condition1 is true 4} 5else if (condition2) 6{ 7 // code to execute if condition2 is true 8} 9else if (condition3) 10{ 11 // code to execute if condition3 is true 12} 13else 14{ 15 // code to execute if none of the conditions are true 16}
Here's an example:
1int x = 0; 2 3if (x > 0) 4{ 5 Console.WriteLine("x is positive."); 6} 7else if (x < 0) 8{ 9 Console.WriteLine("x is negative."); 10} 11else 12{ 13 Console.WriteLine("x is zero."); 14}
In this example, the if
statement checks whether the value of x
is greater than 0. If it is, the code inside the first set of braces is executed, which prints "x is positive." to the console. If it is not, the next else if
statement is tested, which checks whether x
is less than 0. If it is, the code inside the second set of braces is executed, which prints "x is negative." to the console. If it is not, the code inside the third block will execute.
Nested if Statements
You can also use if statements inside other if statements. This is called a nested if statement. The inner if statement is executed only if the outer if statement evaluates to true. Here is an example:
1int num1 = 5, num2 = 10, num3 = 15; 2if (num1 < num2) 3{ 4 Console.WriteLine("num1 is less than num2"); 5 if (num2 < num3) 6 { 7 Console.WriteLine("num2 is less than num3"); 8 } 9}
In this example, the outer if statement checks whether num1
is less than num2
. If it is true, it executes the code inside the if block. Inside the if block, there is another if statement that checks whether num2
is less than num3
. If it is true, it executes the code inside the inner if block.
Nested if statements can be useful in situations where you need to test for multiple conditions. However, be careful not to make your code too complex or difficult to read.