C# if, if. else, if. else if and Nested if Statement
Содержание
- C# if (if-then) Statement
- C# if. else (if-then-else) Statement
- C# if. else if (if-then-else if) Statement
- Nested if. else Statement
In this article, we will learn how to use if, if. else, if. else if statement in C# to control the flow of our program’s execution.
Testing a condition is inevitable in programming. We will often face situations where we need to test conditions (whether it is true or false ) to control the flow of program. These conditions may be affected by user’s input, time factor, current environment where the program is running, etc.
In this article, we’ll learn to test conditions using if statement in C#.
C# if (if-then) Statement
C# if-then statement will execute a block of code if the given condition is true. The syntax of if-then statement in C# is:
- The boolean-expression will return either true or false .
- If the boolean-expression returns true , the statements inside the body of if ( inside <. >) will be executed.
- If the boolean-expression returns false , the statements inside the body of if will be ignored.
In this example, the statement
will be executed only if the value of number is less than 5.
How if statement works?
Working of C# if Statement
Example 1: C# if Statement
When we run the program, the output will be:
The value of number is initialized to 2. So the expression number 5 is evaluated to true . Hence, the code inside the if block are executed. The code after the if statement will always be executed irrespective to the expression.
Now, change the value of number to something greater than 5 , say 10 . When we run the program the output will be:
The expression number 5 will return false , hence the code inside if block won’t be executed.
C# if. else (if-then-else) Statement
The if statement in C# may have an optional else statement. The block of code inside the else statement will be executed if the expression is evaluated to false .
The syntax of if. else statement in C# is:
In this example, the statement
will be executed only if the value of number is less than 5 .
will be executed if the value of number is greater than or equal to 5 .
How if. else Statement works?
Working of if. else Statement
Example 2: C# if. else Statement
When we run the program, the output will be:
Here, the value of number is initialized to 12 . So the expression number 5 is evaluated to false . Hence, the code inside the else block are executed. The code after the if..else statement will always be executed irrespective to the expression.
Now, change the value of number to something less than 5 , say 2 . When we run the program the output will be:
The expression number 5 will return true, hence the code inside if block will be executed.
Ternary operator in C# provides a shortcut for C# if. else statement.
C# if. else if (if-then-else if) Statement
When we have only one condition to test, if-then and if-then-else statement works fine. But what if we have a multiple condition to test and execute one of the many block of code.
For such case, we can use if..else if statement in C#. The syntax for if. else if statement is:
The if. else if statement is executed from the top to bottom. As soon as a test expression is true , the code inside of that if ( or else if ) block is executed. Then the control jumps out of the if. else if block.
If none of the expression is true , the code inside the else block is executed.
Alternatively, we can use switch statement in such condition.
Example 3: C# if. else if Statement
When we run the program, the output will be:
The value of number is initialized to 12 . The first test expression number 5 is false , so the control will move to the else if block. The test expression number 5 is true hence the block of code inside else if will be executed.
Similarly, we can change the value of number to alter the flow of execution.
Nested if. else Statement
An if. else statement can exist within another if. else statement. Such statements are called nested if. else statement.
The general structure of nested if…else statement is:
Nested if statements are generally used when we have to test one condition followed by another. In a nested if statement, if the outer if statement returns true, it enters the body to check the inner if statement.
Example 4: Nested if. else Statement
The following program computes the largest number among 3 numbers using nested if. else statement.
Источник: