C# if else statement

C# have various flow control statements which help us to implement decision based coding and apply various methods on every possible conditions in a given program.

When a programmer executes a code, usually it is executed from top to bottom line by line, but with the help of flow control statements we can alter it and also repeat the same line of code if needed using C# Loops (will discuss in later chapter).

So here are the C# control statements like If, If-else and Nested if-else  defined in this chapter.

If Statement in C#

If keyword can used when you want to check if a condition is true, then execute the code inside if condition, here is the general syntax

if(SomeCondition)
{
   //if above condition evalute to true, then code is executed inside this block
   //some code here
}

Condition, can be a single condition or multiple conditions.

Example:

using System;
					
public class IfStatementProgram
{
	public static void Main()
	{
		int a=10;
		int b=20;
		
		//if condition 1
		if(a < 20)
		{
			//code to be executed if condition is met true
			Console.WriteLine("a is less than 20");
		}
		
		// if condition 2
		if(b < 20)
		{
			//this will not be printed as b = 20 and not less than 20
			Console.WriteLine("b is less than 20");
		}
	}
}

Output

a is less than 20

We can implement a multiple conditions in single if statement also by using logical operators ( && , || ), AND, OR, something like this

using System;
					
public class IfStatementProgram
{
	public static void Main()
	{
		int a=10;
		int b=20;
		
		//if multiple condition
		if(a < 20 && b==20)
		{
			//code to be executed if condition is met true
			Console.WriteLine("a is less than 20 and b is equal to 20");
		}
	
	}
}

Output:

a is less than 20 and b is equal to 20

If-Else Statement in C#

In above examples, we saw we can execute code "if" condition is met to true, now suppose we want to execute a code, when condition inside "if" statement is not equal to true, so that is where "else" keyword helps.

If the expression inside the square brackets following the if keyword evaluates to false, the statement following the else keyword is automatically executed.

Syntax for If-Else Statement

if(ConditionIsTrue)
{
  // execute these lines of code
}
else
{
  // execute these lines of code, if above condition is not true
}

For example:

using System;
					
public class IfElseStatementProgram
{
	public static void Main()
	{
		int a=10;
		
		
		//if condition 
		if(a > 20)
		{
			//code to be executed if condition is met true
			Console.WriteLine("a is greater than 20");
		}
		else
		{
			//this will be printed, as a is less than 20
			Console.WriteLine("a is less than 20");
		}
	}
}

Output:

a is less than 20

As the value of a = 10, and is not greater than 20 else block code will be executed.

Nested If-else-if Statement

The if statement can also be followed by else if..else construct. This is very useful in testing multiple conditions., we can create multiple branches using the if-else (else if) keyword.

The if-else-if-..else keyword tests for another condition if and only if the previous condition was not met.

Syntax

if(Condition1)
{
   //if condition 1 is true, execute this
}
else if (Condition2)
{
   // if condition 2 is true, execute this
}
else if (condition3)
{
   //if condition 3 is true, execute thi
}
else
{
   //if none of the above condition is true
}

Example:

    using System;

     
    namespace if_else
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                int opt, num1, num2;
                float result;
           
     
                Console.WriteLine("Select any one");
                Console.WriteLine("Press 1 for addition");
                Console.WriteLine("Press 2 for subtraction");
                Console.WriteLine("Press 3 for multiplication");
                Console.WriteLine("Press 4 for Division");
				
     
                Console.Write("\n\nEnter first number:\t");
                num1 = Convert.ToInt32(Console.ReadLine());
     
                Console.Write("Enter second number:\t");
                num2 = Convert.ToInt32(Console.ReadLine());
     
                Console.Write("\nEnter your option:\t");
                opt = Convert.ToInt32(Console.ReadLine());
     
                if (opt == 1)
                {
                    result = num1 + num2;
                    Console.WriteLine("\n{0} + {1} = {2}", num1, num2, result);
                }
                else if (opt == 2)
                {
                    result = num1 - num2;
                    Console.WriteLine("\n{0} - {1} = {2}", num1, num2, result);
                }
                else if (opt == 3)
                {
                    result = num1 * num2;
                    Console.WriteLine("\n{0} x {1} = {2}", num1, num2, result);
                }
                else if (opt == 4)
                {
                    result = (float)(num1 / num2);
                    Console.WriteLine("\n{0} / {1} = {2}", num1, num2, result);
                }
				
                
                Console.ReadLine();
            }
        }
    }

Output:

Select any one
Press 1 for addition
Press 2 for subtraction
Press 3 for multiplication
Press 4 for Division


Enter first number:    55
Enter second number:    22

Enter your option:    2

55 - 22 = 33

Share Tweet