Team LiB
Previous Section Next Section

Chapter 4: Program Control

Selection Statements

You can use the if statement or the if else combination to select code to be executed. However, there is a practical limit to the number of selection-based if statements that you can combine. In these cases, if for no other reason than code readability, you should use a switch case statement instead of multiple if statements.

if and if else

By itself, the if statement means that if an expression to be evaluated is true, then execute this/these statements. The if statement evaluates an expression contained within the parentheses.

if(expression)
{
       Statement
       Statement
}

The parentheses help the compiler to identify the expression to be evaluated. If the expression evaluates to true, then the statements in the code block of the if statement are executed. If the expression evaluates to false, then the statements in the code block of the if statement are not executed.

If the if statement is followed by a single statement, the braces after the if statement are optional.

if(expression)
       Statement

An if statement can be used in conjunction with an else statement. This allows the first block to be executed for a true result of the if expression and the second set of statements following the else block to be executed for a false result.

if(expression)
       Statement - Expression evaluates to true
else
       Statement - Expression evaluates to false

You can nest if statements within if statements to produce a series of selection statements.


if(expression)
{
         if(expression)
         {
         }
}

You can also use logical operators (&& and ||) to combine multiple expression statements.

if(expression && expression)

The following example demonstrates the use of if else selection statements.

Code Example: if else
Start example
using System;

namespace Client.Chapter_4___Program_Control
{
      class ifelse
      {
            static void Main(string[] args)
            {
                  int a = 5, b = 5, c = 10;
                  //Compares a to b. If they are equal, then the expression
                  //is true and WriteLine is called.
                  if (a == b)
                        Console.WriteLine(a);
                  else
                        Console.WriteLine(b);
                  //Compares a > c and if a equal to b
                  if ((a > c) || (a == b))
                        Console.WriteLine(b);
                  //If a is greater than or equal to c and
                  //b is less than or equal to c
                  if ((a >= c) && (b <= c))
                        Console.WriteLine(c);
            }
      }
}
End example

switch case

The switch case statement accepts a single integral value in the switch statement, and then compares that value with other values that are defined in the case statement.

switch(value)
{
       case value:
            break;
       default:
            break;
}
Note?/td>

Unlike the C++ implementation of switch case statements, in C#, there is no fall-through between case statements. You must provide a break.

In the following example, a switch statement is declared where the value examined is the variable a. The execution will then flow through the case statements until it gets to the case statement that matches the value inside the switch statement.

Code Example: switch case
Start example

using System;
namespace Client.Chapter_4___Program_Control
{
      class MyMainClass
      {
            static void Main(string[] args)
            {
                  int a = 0;
                  Console.ReadLine();

                  //Evaluates the value of a and compares to the value of the case
                  //statements below
                  switch (a)
                  {
                        case 1:
                              Console.WriteLine("One");
                              break;
                        case 2:
                              Console.WriteLine("Two");
                              break;
                        //Default is called when no match is made
                        default:
                              Console.WriteLine("?");
                              break;
                  }
            }
      }
}
End example

Team LiB
Previous Section Next Section