Home Basics C# if…else statements

C# if…else statements

The if statement evaluates its condition expression to determine whether to execute the if-body.

Optionally, an else clause can immediately follow the if body, providing code to execute when the condition is false.

Making the else-body another if statement creates the common cascade of if, else if, else if, else if, else statements

 

if Statement

This is the syntax of the if statement

if(condition)
{  
//code to be executed  
}

Example

If the variable x is greater than 10 display a message, it is.

using System;

public class Program
{
	public static void Main()
	{
		int x = 20;
		if (x >= 10)
		{
			Console.WriteLine("x is Greater than 10");
		}
		Console.WriteLine("Press Enter Key to Exit..");
		Console.ReadLine();
	}
}

You should see the following

x is Greater than 10
Press Enter Key to Exit..

Change the above example so that if (x <= 10) and see what happens

if-else Statement

Use the else statement to specify a block of code t that will be executed if the condition is False.

This is the syntax of the if-else statement

if(condition)
{  
    //code if condition is true  
}
else
{  
    //code if condition is false  
}

lets see an example of this

Example

 

using System;

public class Program
{
	public static void Main()
	{
		int x = 20;
		if (x <= 10)
		{
			Console.WriteLine("x is greater than 10");
		}
		else
		{
			Console.WriteLine("x is less than 10");
		}
		Console.WriteLine("Press Enter Key to Exit..");
		Console.ReadLine();
	}
}

You should see the following

x is less than 10
Press Enter Key to Exit..

If else-if Example

Use the else if statement to specify a new condition to be checked if the first condition is False.

This is the syntax of the if else-if statement

if(condition1)
{  
    //code to be executed if condition1 is true  
}
else if(condition2)
{  
    //code to be executed if condition2 is true  
}  
    
else if(condition3)
{  
    //code to be executed if condition3 is true  
}  
...  
else
{  
    //code to be executed if all the conditions are false  
}

A similar example as before

 

using System;

public class Program
{
	public static void Main()
	{
		int x = 10;
		if (x > 10)
		{
			Console.WriteLine("x is greater than 10");
		}
		else if(x < 10)
		{
			Console.WriteLine("x is less than 10");
		}
		else
		{
			Console.WriteLine("x is 10");
		}
		Console.WriteLine("Press Enter Key to Exit..");
		Console.ReadLine();
	}
}

Which will result in

x is 10
Press Enter Key to Exit..

Here is another example, this is your fairly typical console grade program where a user inputs a grade and you use an if else-if to display a message to the user depending on the grade.

 

using System;

public class Program
{
	public static void Main()
	{  
		Console.WriteLine("Enter a number to check grade:");  
		int num = Convert.ToInt32(Console.ReadLine());  

		if (num <0 || num >100)  
		{  
			Console.WriteLine("wrong number");  
		}  
		else if(num >= 0 && num < 50){  
			Console.WriteLine("Fail");  
		}  
		else if (num >= 50 && num < 60)  
		{  
			Console.WriteLine("D Grade");  
		}  
		else if (num >= 60 && num < 70)  
		{  
			Console.WriteLine("C Grade");  
		}  
		else if (num >= 70 && num < 80)  
		{  
			Console.WriteLine("B Grade");  
		}  
		else if (num >= 80 && num < 90)  
		{  
			Console.WriteLine("A Grade");  
		}  
		else if (num >= 90 && num <= 100)  
		{  
			Console.WriteLine("A+ Grade");  
		}  
	}  
}

Here is a run of this program

Enter a number to check grade:
74
B Grade

Nested if…else Statement

You can also nest if…else statements, lets look at the syntax

 

if (condition1)
{
	if (condition2)
	{
		// code to be executed if condition2 is true
	}
	else
	{
	        // code to be executed if condition2 is false
	}
}
else
{
	if (condition3)
	{
		// code to be executed if condition3 is true
	}
	else
	{
		// code to be executed if condition3 is true
	}
}

Nested if statements are 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.

Lets see an example of this

using System;

public class Program
{
	public static void Main()
	{
		int x = 10, y = 30;
		if (x > y)
		{
			if (x >= 20)
			{
				Console.WriteLine("x is greater than or equal to 10");
			}
			else
			{
				Console.WriteLine("x is less than 10");
			}
		}
		else
		{
			if (y <= 20)
			{
				Console.WriteLine("y is less than or equal to 20");
			}
			else
			{
				Console.WriteLine("y is greater than 20");
			}
		}
		Console.WriteLine("Press Enter Key to Exit..");
		Console.ReadLine();
	}
}

Run this and you will see the following

y is greater than 20
Press Enter Key to Exit..

You may also like