Multiple Selection if Statements

[print-me]

What if you have more conditions to test? You can write multiple if Statements but wouldn’t it be better if you can combine those if statements into one whole list. You can have multiple selections in an if statement using the syntax below.

if (condition)
{
   code to execute;
}
else
{
   if (condition)
   {
      code to execute;
   }
   else
   {
      if (condition)
      {
         code to execute;
      }
      else
      {
         code to execute;
      }
   }
}

The code above is hard to read. You are branching if statements inside the else block. There’s a better way of doing this.  We could rewrite the code above and make it simpler.

if (condition)
{
   code to execute;
}
else if (condition)
{
   code to execute;
}
else if (condition)
{
   code to execute;
}
else
{
   code to execute;
}

We introduce using the else if statement. Like the else, the else if must have a corresponding if statement. The else ifstatement executes if the first if statement is not true. The condition of the else if is then tested and if false, the other else ifstatements will be executed. If no other else if is found, the else block will be executed instead. Let’s make a program that will demonstrate the else if statements.

using System;

namespace MultipleSelectionIfStatementsDemo
{
    public class Program
    {
        public static void Main()
        {
            int choice;

            Console.WriteLine("What's your favorite color?");
            Console.WriteLine("[1] Black");
            Console.WriteLine("[2] White");
            Console.WriteLine("[3] Blue");
            Console.WriteLine("[4] Red");
            Console.WriteLine("[5] Yellow\n");

            Console.Write("Enter your choice: ");
            choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 1)
            {
                Console.WriteLine("You might like my black t-shirt.");
            }
            else if (choice == 2)
            {
                Console.WriteLine("You might be a clean and tidy person.");
            }
            else if (choice == 3)
            {
                Console.WriteLine("You might be sad today.");
            }
            else if (choice == 4)
            {
                Console.WriteLine("You might be inlove right now.");
            }
            else if (choice == 5)
            {
                Console.WriteLine("Lemon might be your favorite fruit.");
            }
            else
            {
                Console.WriteLine("Sorry, your favorite color is " +
                                  "not in the choices above.");
            }
        }
    }
}

Example 1 – Using Multiple Selection if Statements

What's your favorite color?
[1] Black
[2] White
[3] Blue
[4] Red
[5] Yellow

Enter your choice: 1
You might like my black t-shirt.
What's your favorite color?
[1] Black
[2] White
[3] Blue
[4] Red
[5] Yellow

Enter your choice: 999
Sorry, your favorite color is not in the choices above.

The output will vary depending on what choice you give to the program. Depending on what you choose, the program will print a different message. Take note about the else statement. If the number you typed is not in the choices, then the code inside the elseblock will be executed.