Syntax for Nested If


 if (condition)
{
        Statements
        .
        .
}
else if (condition)
{
        Statements
        .
        .
}
.
.
else
{
        Statements
        .
        .
}




The nested if consist of multiple statements. The nested if start with the if statement, which is called the outer if statement, and contains multiple if statements, which are called inner if statements. In the nested if statements, the outer if controls the execution of the inner if statements. The compiler executes the inner if statement only if the condition in the outer if statement is true. In addition, each inner if statement is executed only if the condition in its previous inner if statement is true



Example :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CABasics
{
    class NIfsExample
    {
        public static void Main()
        {
            Console.WriteLine("Enter an alphabet");
            string s = Console.ReadLine();
                
               if(s=="a")
                    Console.WriteLine("You Have Entered an Vowel");
               else if(s=="e")
                    Console.WriteLine("You Have Entered an Vowel");
               else if(s=="i")
                    Console.WriteLine("You Have Entered an Vowel");
               else if(s=="o")
                    Console.WriteLine("You Have Entered an Vowel");
               else if(s=="u")
                    Console.WriteLine("You Have Entered an Vowel");
                   
               else
                    Console.WriteLine("Not a vowel");
                    
            Console.ReadLine();
        }
    }
}

  output:-





No comments:

Post a Comment