C Sharp if else

C# if-else

To test a condition or multiple conditions, C# provides various types of if statements. These are:

  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder

C# IF Statement:

The C# IF statement is used to test the condition and then to execute the specified code if the condition is true.

Syntax:

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

Example:

using System;      
public class Example  
    {  
       public static void Main(string[] args)  
        {  
            int n = 20;  
            if (n % 2 == 0)  
            {  
                Console.WriteLine("Even Number!!");  
            }  
 
        }  
    }

Output:

Even Number!!

Explanation:

In the above example, we are displaying the working and behavior of the C# if statement.

C# if-else Statement:

The C# if-else statement is used to test the condition and to execute the IF block if the condition is true, or to execute the else block if the condition is false.

Syntax:

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

Example 1:

using System;      
public class Example  
    {  
       public static void Main(string[] args)  
        {  
            int n = 21;  
            if (n % 2 == 0)  
            {  
                Console.WriteLine("Even Number!!");  
            }  
            else  
            {  
                Console.WriteLine("Odd Number!!");  
            }  
        }  
    }

Output:

Odd Number!!

Explanation:

In the above example, we are displaying the working and behavior of the C# if-else statement.

Example 2: With input from the user.

using System;      
public class Example  
    {       
       public static void Main(string[] args)  
        {  
            Console.WriteLine("Enter a number:");  
            int n = Convert.ToInt32(Console.ReadLine()); 
 
            if (n % 2 == 0)  
            {  
                Console.WriteLine("Even Number!!");  
            }  
            else  
            {  
                Console.WriteLine("Odd Number!!");  
            }  
        }  
    }

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the working and behavior of the C# if-else statement, while taking input from the user. The Console.ReadLine() method is used for this which returns a string. The string is converted into an int using Convert.ToInt32() method, to get a numeric value.

C# if-else-if ladder Statement:

To execute one condition from multiple statements, the C# if-else-if ladder statement is used.

Syntax:

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  
}  

Example:

using System;      
public class Example  
    {  
        public static void Main(string[] args)  
        {  
            Console.WriteLine("Enter a number between 0 to 100:");  
            int n = Convert.ToInt32(Console.ReadLine());  
 
            if(n >= 0 && n < 20){  
                Console.WriteLine("YOU HAVE TO SMILE NOW!!");  
            }  
            else if (n >= 20 && n < 40)  
            {  
                Console.WriteLine("YOU HAVE TO LAUGH NOW!!");  
            }  
            else if (n >= 40 && n < 60)  
            {  
                Console.WriteLine("YOU HAVE TO CRY NOW!!");  
            }  
            else if (n >= 60 && n < 80)  
            {  
                Console.WriteLine("YOU HAVE TO SHOUT NOW!!");  
            }  
            else if (n >= 80 && n < 100)  
            {  
                Console.WriteLine("YOU HAVE TO SING NOW!!");  
            }  
            else 
            {  
                Console.WriteLine("Enter again."); 
            }  
        }  
    }

Output 1:

Output 2:

Output 3:

Explanation:

In the above example, we are displaying the working and behavior of the C# if-else-if ladder statement.

 

Please follow and like us:
Content Protection by DMCA.com