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-then statement will execute a block of code if the given condition is true. The syntax of if-then statement in C# is:
if (boolean-expression) { // statements executed if boolean-expression is true }
true
or false
.true
, the statements inside the body of if ( inside {...}
) will be executed.false
, the statements inside the body of if will be ignored.For example,
if (number < 5) { number += 5; }
In this example, the statement
number += 5;
will be executed only if the value of number is less than 5.
using System;
namespace Conditional {
class IfStatement {
public static void Main(string[] args) {
int number = 2;
if (number < 5) {
Console.WriteLine("{0} is less than 5", number);
}
Console.WriteLine("This statement is always executed.");
}
}
}
When we run the program, the output will be:
2 is less than 5 This statement is always executed.
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:
This statement is always executed.
The expression number < 5
will return false
, hence the code inside if block won’t be executed.
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:
if (boolean-expression) { // statements executed if boolean-expression is true } else { // statements executed if boolean-expression is false }
For example,
if (number < 5) { number += 5; } else { number -= 5; }
In this example, the statement
number += 5;
will be executed only if the value of number is less than 5
.
The statement
number -= 5;
will be executed if the value of number is greater than or equal to 5
.
using System;
namespace Conditional {
class IfElseStatement {
public static void Main(string[] args) {
int number = 12;
if (number < 5) {
Console.WriteLine("{0} is less than 5", number);
}
else {
Console.WriteLine("{0} is greater than or equal to 5", number);
}
Console.WriteLine("This statement is always executed.");
}
}
}
When we run the program, the output will be:
12 is greater than or equal to 5 This statement is always executed.
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:
2 is less than 5 This statement is always executed.
The expression number < 5
will return true, hence the code inside if block will be executed.
true
, the code inside of that if ( or else if ) block is executed. Then the control jumps out of the if…else if block.</p>
<p>If none of the expression is true
, the code inside the else block is executed.</p>
<h4>Example 3: C# if…else if Statement</h4>
<pre>using System;
namespace Conditional {
class IfElseIfStatement {
public static void Main(string[] args) {
int number = 12;
if (number < 5) {
Console.WriteLine("{0} is less than 5", number);
}
else if (number > 5) {
Console.WriteLine("{0} is greater than 5", number);
}
else {
Console.WriteLine("{0} is equal to 5");
}
}
}
}
</pre>
<p>When we run the program, the output will be:</p>
<pre>12 is greater than 5</pre>
<p>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.</p>
<p>Similarly, we can change the value of number
to alter the flow of execution.</p>
<hr />
<h3>Nested if…else StatementAn 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:
if (boolean-expression) { if (nested-expression-1) { // code to be executed } else { // code to be executed } } else { if (nested-expression-2) { // code to be executed } else { // code to be executed } }
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.
The following program computes the largest number among 3 numbers using nested if…else statement.
using System;
namespace Conditional {
class Nested {
public static void Main(string[] args) {
int first = 7, second = -23, third = 13;
if (first > second) {
if (firstNumber > third) {
Console.WriteLine("{0} is the largest", first);
}
else {
Console.WriteLine("{0} is the largest", third);
}
}
else {
if (second > third) {
Console.WriteLine("{0} is the largest", second);
}
else {
Console.WriteLine("{0} is the largest", third);
}
}
}
}
}
When we run the program, the output will be:
13 is the largest
In the editor below, perform the different tasks as directed.
If the number is odd, print ‘Odd’, otherwise print ‘Even’ (without quotes).
</div>