Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

C# if elseif else Statement


In the previous C# tutorials, we have discussed the C# if statement and the C # if-else statement . If you do not know about the if
statement and if-else, then, first of all, learn these tutorials only then you can understand if ... elseif ... else statement. In C# if statement and C# if-else statement we can execute code by creating only one condition but in the C# if ... elseif ... else statement code can be executed by creating two or more than two conditions .C# if ... elseif ... else can execute different codes for two or more conditions.

 

Let's create an example for C# if ... elseif ... else statement.


using System;      
public class IfelsifelseExample  
    {  
        public static void Main(string[] args)  
        {  
            int no=92;  /* Change numer and execute */
  
            if (no <0 || no >100)  
            {  
Console.WriteLine("Invalid number. You are typed " + no+ ".  Try number 100 or below 100.");  
            }  
            else if(no >= 0 && no < 50){  
                Console.WriteLine("You are fail. You have got numbers= "+no);  
            }  
            else if (no >= 50 && no < 60)  
            {  
                Console.WriteLine("Oh! D Grade.You have got numbers= "+no);  
            }  
            else if (no >= 60 && no < 70)  
            {  
Console.WriteLine(" You are passed with C Grade. You have got numbers= "+no);  
            }  
            else if (no >= 70 && no < 80)  
            {  
                Console.WriteLine("Nice ! B Grade. You have got numbers= "+no);  
            }  
            else if (no >= 80 && no < 90)  
            {  
                Console.WriteLine("Great! A Grade.You have got numbers= "+no);  
            }  
            else if (no >= 90 && no <= 100)  
            {  
           Console.WriteLine("Cool ! A+ Grade. You have got numbers= "+no);  
            }  
           }
        }  

Output -

Cool! A+ Grade. You have got numbers= 92


In the above C# example, we created many conditions using C# if...elseif...else Statement. If the value of no is below 0 or above 100 it displays the statement invalid number . if the value is above 0 and below 50, it displays "You are fail." . If the value of no is above 50 and below 60, it displays the Grade D with that number.

 

Follow this process. The compiler checks the condition if the condition is evaluated to true then execute the code, if the condition is evaluated to false. the compiler checks other conditions. It is the structure of C# The if...elseif...else Statement.

Its mostly used and very essential in C# programming.

 


Please Share

Recommended Posts:-