Dark Mode
Image

C# Properties

C# Inheritance

C# Polymorphism

C# Strings

C# Generics

C# Delegates

C# Reflection

Anonymous Function

C# Multithreading

C# Synchronization

C# Web Service

C# Misc

C# New Features

C# Programs

ADO.NET Tutorial

ASP.NET Tutorial

C# if-else

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:

 if(condition)
{  
//code to be executed  
}  
 
if statement in java

C# If Example

using System;      
public class IfExample  
 {  
   public static void Main(string[] args)  
                   int num = 10;  
           if (num % 2 == 0)    
          {  
                Console.WriteLine("It is even number");  
           } 
                     
    }  

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:

 if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}  
C# if-else statement

C# If-else Example

 using System;      

public class IfExample  

    {  

     public static void Main(string[] args)  

    { 

    int num = 11;  

         if (num % 2 == 0)  

            {  

                Console.WriteLine("It is even number");  

            }  

            else  

            {  

     Console.WriteLine("It is odd number");  

            }  

              

        }  

    }  

 

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.

using System;     

public class IfExample 

    { 

       public static void Main(string[] args) 

        { 

            Console.WriteLine("Enter a number:"); 

            int num = Convert.ToInt32(Console.ReadLine()); 

 

            if (num % 2 == 0) 

            { 

                Console.WriteLine("It is even number"); 

            } 

            else 

            { 

                Console.WriteLine("It is odd number"); 

            } 

             

        } 

    } 

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:
 

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 

C# if-else-if statement

C# If else-if Example

using System;     

public class IfExample 

    { 

        public static void Main(string[] args) 

        { 

            Console.WriteLine("Enter a number to check grade:"); 

            int num = Convert.ToInt32(Console.ReadLine()); 

 

            if (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"); 

            } 

        } 

    } 

Output:

 

Enter a number to check grade:66 C Grade
 

Output:
 

Enter a number to check grade:-2
wrong number

 

 

Comment / Reply From