Home » C# » C# programs » C# basic programs

C# program to demonstrate example of nested if else statement

C# nested if else statement example: Here, we are writing a C# program to demonstrate example of nested if else statement.
Submitted by IncludeHelp, on April 07, 2019

Like other programming languages, in C# also conditions may be checked within a condition, i.e. nested if-else allows us to check other conditions within if or else block.

Syntax:

    if(test_condition1){
	    //code section 1
	    if(test_condition_a){
		    //code section a
    }
    else{
	    //code section b
    }
    }
    else if(test_condition2){
    {
	    //code section 2
    }
    else if(test_condition3){
	    //code section 3
    }
    ...
    else{
	    //else code section
    }

See the syntax above, here we are checking test_condition_a within the test_conditon1 block, if test_condition1 will be true, "code section a" will be executed.

C# example for nested if else statement

Here, we are asking for a character – and check whether it is vowel or consonant, if input character is a valid alphabet

// C# program to demonstrate example of 
// nested if else statement 
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //input a character and check whether it is vowel or consonant 
            //but before it - check input character is an aplhabet or not
            char ch;
            Console.Write("Enter a character: ");
            ch = Console.ReadLine()[0];

            //check ch is an alphabet or not
            if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')){
                Console.WriteLine("{0} is a valid alphabet", ch);
                //checking for vowel or consonant
                if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' ||
                    ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' ||
                    ch == 'U' || ch == 'u')
                {
                    Console.WriteLine("{0} is a vowel", ch);
                }
                else
                {
                    Console.WriteLine("{0} is a consonant", ch);
                }
            }
            else{
                Console.WriteLine("{0} is not a valid alphabet",ch);
            }
			
            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

First run:
Enter a character: X
X is a valid alphabet
X is a consonant

Second run:
Enter a character: u
u is a valid alphabet
u is a vowel

Third run:
Enter a character: $
$ is not a valid alphabet





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.