C# program to demonstrate an example of multiple if-else statement

Syntax:

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

1) C# example 1 for multiple if else statement:

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

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //input an integer number and check whether 
            //it is postive, negative or zero
            int number;
            
            Console.Write("Enter an integer number: ");
            number = Convert.ToInt32(Console.ReadLine());

            //checking conditions
            if (number > 0)
                Console.WriteLine("{0} is a positive number", number);
            else if (number < 0)
                Console.WriteLine("{0} is a negative number", number);
            else
                Console.WriteLine("{0} is a Zero", number);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

Enter an integer number: -123
-123 is a negative number

2) C# example 2 for multiple if-else statement:

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

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //input gender and check for "Male", "Female" or "Unspecied gender"
            string gender = "";

            Console.Write("Enter gender: ");
            gender = Console.ReadLine();

            if (gender.ToUpper() == "MALE")
                Console.WriteLine("He is male");
            else if (gender.ToUpper() == "FEMALE")
                Console.WriteLine("She is female");
            else
                Console.WriteLine("Unspecified gender");

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

First run:
Enter gender: male
He is male

Second run:
Enter gender: FEMale
She is female

Third run:
Enter gender: Don't know
Unspecified gender

Leave a Comment

error: Alert: Content is protected!!