C Sharp If Else

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# if-else

In C# programming, the if statement is used to test the condition. There are various types of if statements in C#.

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

C# IF Statement

The C# if statement tests the condition. It is executed if condition is true.

Syntax:

  1. if(condition){  
  2. //code to be executed  
  3. }  

if statement in java

C# If Example

  1. using System;      
  2. public class IfExample  
  3.     {  
  4.        public static void Main(string[] args)  
  5.         {  
  6.             int num = 10;  
  7.             if (num % 2 == 0)  
  8.             {  
  9.                 Console.WriteLine("It is even number");  
  10.             }  
  11.               
  12.         }  
  13.     }  

Output:

It is even number

C# IF-else Statement

The C# if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

Syntax:

  1. if(condition){  
  2. //code if condition is true  
  3. }else{  
  4. //code if condition is false  
  5. }  

C# if-else statement

C# If-else Example

  1. using System;      
  2. public class IfExample  
  3.     {  
  4.         public static void Main(string[] args)  
  5.         {  
  6.             int num = 11;  
  7.             if (num % 2 == 0)  
  8.             {  
  9.                 Console.WriteLine("It is even number");  
  10.             }  
  11.             else  
  12.             {  
  13.                 Console.WriteLine("It is odd number");  
  14.             }  
  15.               
  16.         }  
  17.     }  

Output:

It is odd number

C# If-else Example: with input from user

In this example, we are getting input from the user using Console.ReadLine() method. It returns string. For numeric value, you need to convert it into int using Convert.ToInt32() method.

  1. using System;      
  2. public class IfExample  
  3.     {  
  4.        public static void Main(string[] args)  
  5.         {  
  6.             Console.WriteLine("Enter a number:");  
  7.             int num = Convert.ToInt32(Console.ReadLine());  
  8.   
  9.             if (num % 2 == 0)  
  10.             {  
  11.                 Console.WriteLine("It is even number");  
  12.             }  
  13.             else  
  14.             {  
  15.                 Console.WriteLine("It is odd number");  
  16.             }  
  17.               
  18.         }  
  19.     }  

Output:

Enter a number:11
It is odd number

Output:

Enter a number:12
It is even number

C# IF-else-if ladder Statement

The C# if-else-if ladder statement executes one condition from multiple statements.

Syntax:

  1. if(condition1){  
  2. //code to be executed if condition1 is true  
  3. }else if(condition2){  
  4. //code to be executed if condition2 is true  
  5. }  
  6. else if(condition3){  
  7. //code to be executed if condition3 is true  
  8. }  
  9. ...  
  10. else{  
  11. //code to be executed if all the conditions are false  
  12. }

C# if-else-if statement

C# If else-if Example

  1. using System;      
  2. public class IfExample  
  3.     {  
  4.         public static void Main(string[] args)  
  5.         {  
  6.             Console.WriteLine("Enter a number to check grade:");  
  7.             int num = Convert.ToInt32(Console.ReadLine());  
  8.   
  9.             if (num <0 || num >100)  
  10.             {  
  11.                 Console.WriteLine("wrong number");  
  12.             }  
  13.             else if(num >= 0 && num < 50){  
  14.                 Console.WriteLine("Fail");  
  15.             }  
  16.             else if (num >= 50 && num < 60)  
  17.             {  
  18.                 Console.WriteLine("D Grade");  
  19.             }  
  20.             else if (num >= 60 && num < 70)  
  21.             {  
  22.                 Console.WriteLine("C Grade");  
  23.             }  
  24.             else if (num >= 70 && num < 80)  
  25.             {  
  26.                 Console.WriteLine("B Grade");  
  27.             }  
  28.             else if (num >= 80 && num < 90)  
  29.             {  
  30.                 Console.WriteLine("A Grade");  
  31.             }  
  32.             else if (num >= 90 && num <= 100)  
  33.             {  
  34.                 Console.WriteLine("A+ Grade");  
  35.             }  
  36.         }  
  37.     }  

Output:

Enter a number to check grade:66
C Grade

Output:

Enter a number to check grade:-2
wrong number