Selection Statements


  • A selection statement is a control statement that allows for choosing between two or more execution paths in a program on the basis of some condition.
  • C# includes one-way, two-way and n-way (multiple) selectors.
    • An if statement with no else is a one way selector.
    • An if..else statement is a two way selector.
    • if..elseif..else, nested if and switch statements are n-way selectors.

C# includes the following selection statements:


  • If statement
  • If..else statement
  • If..elseif..else statement
  • Nested if statement
  • switch-case statement
  • nested switch-case statement

Now we are going to explain each selection statement in brief:


The if statement


  • if statement is a most powerful decision making statement.
  • It allows the user to execute different path of logic on the basis of a given condition.
  • If the condition is true the block of statements present in the body of if statement get executed and if condition is false then other set of statement after if structure get executed.
  • Here is the syntax of if statement given below:
  • Syntax

    if(Boolean expression)
    (
    Body of if statement
    }

  • Here is an example of if statement please goes through it:
  • Example

    using System;
    namespace Selection
    (
    class ifstatement
    (
    static void Main()
    (
    int a = 10;
    int b = 12;
    if (a < b)
    (
    Console.WriteLine("You are inside the if structure");
    Console.WriteLine("\nYes value of a is less than b");
    }
    Console.WriteLine("\nYou are outside the if strurcture");
    Console.ReadLine();
    }
    }
    }

In the above program the value of a is less than b so the condition of if statement will be true that is why statements present inside the body of if statement will be executed followed by the statements present outside the if block.so the Output of the above program would be:


Output

You are inside the if structure
Yes value of a is less than b
You are outside the if structure

The if.. else statement


  • If..else statement contains the two blocks if block and else block.
  • When condition present in parenthesis of if statement get true then statements of if block executed and if condition is false then statements of else block executed.
  • Here is the syntax of if..else statement:
  • Syntax

    if(Boolean expression)
    (
    Body of if
    }
    else
    (
    Body of else
    }

  • Here is an example of if...else statement please go through it:
  • Example

    using System;
    namespace Selection
    (
    class ifelsestatement
    (
    static void Main()
    (
    int a = 10;
    int b = 12;
    if (a > b)
    (
    Console.WriteLine("You are inside the if block");
    Console.WriteLine("\nYes value of a is greater than b");
    }
    else
    (
    Console.WriteLine("You are inside the else block");
    Console.WriteLine("\nNo,value of a is not greater than b");
    }
    Console.WriteLine("\nYou are outside the if...else strurcture");
    Console.ReadLine();
    }
    }
    }

In the above program we see that the a is not greater than b so condition get false so in this case else block will get executed followed by the statement present outside the if-else block,so the Output of the above program would be:


Output

:
You are inside the else block
No, value of a is not greater than a
You are outside the if...else structure

The if...elseif...else statement


  • The if...elseif...else statement is very useful in the case where user want to select a option from multiple options. In this type of statement 'if' is followed by one or more elseif statements and finally end with a else statement.
  • The basic syntax of the if...elseif...else statement is given below:
  • Syntax

    if(Boolean Expression)
    (
    Statements
    ............
    }
    else if(Boolean Expression)
    (
    Statements
    ..........
    }
    else if(Boolean Expression)
    (
    Statements
    .............
    }
    .
    .
    .
    else
    (
    Statements
    ...............
    }

  • Here is an example of if...elseif...else statement
  • Example

    using System;
    namespace Selection
    (
    class ifelseifelsestatement
    (
    static void Main()
    (
    int a = 10;
    int b = 12;
    if (a == b)
    (
    Console.WriteLine("a is equal to b");
    }
    else if (a < b)
    (
    Console.WriteLine("\na is less than b");
    }
    else
    (
    Console.WriteLine("\na is greater than b");
    }
    Console.ReadLine();
    }
    }
    }

In the above program the value of a is less than b so the condition present in parenthesis of 'else if' statement is true that is why the block of 'else if' will execute

  • The Output of the above program is given below:


    Output

    :
    a is less than b

    The Nested if statement


    • A nested if is an 'if' statement that is the target of another if or else statements.
    • When we use if, if..else if..else, if..else statements inside the other if, else if or else statements that is known as nested if statements.
    • The syntax of the nested if statement is given below:
    • Syntax

      if(Boolean expression)
      (
      ...........
      if(Boolean expression)
      (
      ............
      }
      else
      (
      ......
      }
      }
      else
      (
      .............
      If(Boolean expression)
      (
      ............
      }
      else
      (
      ............
      }
      }

    A nested if statement contains various structures like if..else statement inside if, if statement inside else etc.
    Here is an example of a nested if statement:


    Example

    using System;
    namespace Selection
    (
    class nestedifstatement
    (
    static void Main()
    (
    int a = 10;
    int b = 12;
    int c = 15;
    if (c>a)
    (
    Console.WriteLine("c is greater than a");
    if(c>b) (
    Console.WriteLine("\nc is also greater than b");
    }
    }
    Console.ReadLine();
    }
    }

    In the above program value of c is greater than a and b both. So condition in outer if statement that is (c>a) is true so block of outer if statement will executed and again the condition of inner if statement is also true so block of inner if statement will also executed. The Output of the above program is given below:


    Output

    :
    c is greater than a
    c is also greater than b

    The switch-case statement


    • The switch case statement is also used for selection from multiple expressions like if..elseif..else statement but it is a better approach then if...elseif..if statement.
    • Switch statement contains a expression in its parenthesis it can be number, character or string.One switch statement is followed by several case statements.
    • The switch operator compares the expression to every value listed in the case labels in the body of the switch structure. If a match is found in a case label, the corresponding structure is executed (simple or complex). If no match is found, the default statement is executed (when such exists).
    • The basic syntax for the switch-case statement is given below:
    • Syntax

      Switch(expression)(
      case constant1:
      statements
      ........
      break;
      case constant2:
      statements
      ........
      break;
      .
      .
      .
      case constantN:
      statements
      ........
      break;
      default:
      statements
      .......
      break;
      }

    Rules for switch-case statements


    • Expression present in parenthesis of switch statement must be char, byte, short, int, enum or String. Floating point value cannot be a expression.
    • case constant must be compatible to the expression of the switch statement.
    • No case constant in one switch can have an identical value.
    • When the expression is equal to a case label, the statements following that case will execute until a break statement is reached.
    • default is an optional statement which executed when expression does not match with any case label.
    • Here is an example of switch-case statement:
    • Example

      using System;
      namespace Selection
      (
      class switchcasestatement
      (
      static void Main()
      (
      int a = 2;
      switch (a)
      (
      case 1:
      Console.WriteLine("Value of a is 1");
      break;
      case 2:
      Console.WriteLine("Value of a is 2");
      break;
      case 3:
      Console.WriteLine("Value of a is 3");
      break;
      case 4:
      Console.WriteLine("Value of a is 4");
      break;
      default:
      Console.WriteLine("No match found");
      break;
      }
      Console.ReadLine();
      }
      }
      }

    In the above program switch case check the value of variable against different cases. It finally matches with second case so the statement present in case 2 get executed and the Output of the above program is given below:


    Output

    Value of a is 2

    Nested switch-case statement


    • Whenever switch-case statements are used inside another switch case statement that is known as nested switch case statement.
    • The basic syntax of nested switch-case statement is given below:
    • Syntax

      switch(expression)//outer switch
      (
      case constant1:
      switch(expression)//inner switch
      (
      case constant a:
      statements
      .........
      break;
      case constant b:
      statements
      .......
      break;
      .
      .
      case constant z:
      statements
      ........
      break;
      }
      case constant2:
      statements
      ........
      break;
      .
      .
      Case constant n
      statements
      ......
      break;
      }

    • Here is an example of nested switch-case statement:
    • Example

      using System;
      namespace Selection
      (
      class switchcasestatement
      (
      static void Main()
      (
      char a = 'A';
      char b = 'C';
      switch (a)
      (
      case 'A':
      Console.WriteLine("It is a outer part of switch with chracter A");
      switch (b)
      (
      case 'C':
      Console.WriteLine("It is a inner part of switch with character C");
      break;
      case 'D':
      Console.WriteLine("It is a inner part of switch with character D");
      break;
      }
      break;
      case 'B':
      Console.WriteLine("It is a outer part of switch with character B");
      break;
      }
      Console.ReadLine();
      }
      }
      }

    In the above program the expression present in outer switch statement is matches with the case 'A' so the block of case 'A' will get executed .Inside the case 'A' there is another switch statement whose expression matches with the case 'C' so the block of case 'C' will executed. So the Output of the above program is as follows:


    Output

    It is a outerr part of switch with character 'A'
    It is a inner part of switch with character 'C'