Selection Statements in Java and C#

Photo by ANIRUDH on Unsplash

Selection Statements in Java and C#

All the Java (and C#) programs considered in the previous article consisted of simple instructions, such as declarations and assignments, as well as arithmetic expressions, performed in sequential order. Even though this could suffice for developing basic programs, writing software that aims to solve more complex problems does require the usage of additional types of instructions.

In our previous article we also introduced the concept of logical expression, namely a particular type of expression whose evaluation produces a boolean value as result.

Selection statements are specific constructs provided by a programming language that allow different (groups of) instructions to be executed according to the result of the evaluation of a logical expression.

if statement

The simplest way to control the flow of a program is to execute or ignore (a group of) instructions according to the result of the evaluation of a logical expression. This can be done via the introduction of an if statement

if (CONDITION) // logical expression
{ 
  // block of statements executed if CONDITION is true
}

After the if keyword, a CONDITION—a logical expression—is specified, enclosed in parenthesis. Curly braces are used here to mark the beginning { and the end } of the block of instructions that belong to the if statement. Those instructions will only be executed when the evaluation of the given CONDITION returns true.

if-else statement

This construct is similar to the if statement; however, an additional block of instructions is provided after the else keyword

if (CONDITION) 
{ 
  // block1: statements executed if CONDITION is true
} 
else 
{
  // block2: statements executed if CONDITION is false
}

When the specified CONDITION is evaluated, block1 is executed if the result of the evaluation is true; block2, specified after the else keyword is ignored. Conversely, block1 is ignored, and block2 is executed when the result of the evaluation of CONDITION is false

class Example
{
    public static void main(String[] args) 
    { 
        int temperature = 25 // a value is assigned

        if (temperature > 40)
        {
          System.out.println("Weather is hot");
        } 
        else 
        {
          System.out.println("Weather is not hot");
        }
    }
}

Because in the above example the value assigned to the temperature variable is 25, the program's execution flow will not include the instructions of the if block, and the control will jump to the else block instead. Hence, the program will print "Weather is not hot" on the screen. This is done via the instruction System.out.println provided by the Java Class Library (JCL) as part of the Java Development Platform (JDK).

if-else-if statement

Another selection statement can be built by combining and nesting multiple if-else statements described above, according to the following structure

if (CONDITION1) 
{ 
    // block1: executed if CONDITION1 is true
} 
else if (CONDITION2) 
{
    // block2: executed if CONDITION2 is true
} 
else if (CONDITION3) 
{
    // block3: executed if CONDITION3 is true
}

  // ... more else if blocks

else 
{
    // blockN: executed if none of the above conditions is true
}

According to the above structure, when a program execution reaches the beginning of the if-else-if statement, CONDITION1 would be first evaluated; block1 would be executed only if the result of the evaluation is true. Otherwise, the instructions within block1 are ignored and CONDITION2 is evaluated. Now, block2 is executed if the result of the evaluation of CONDITION2 is true; otherwise, the block is ignored, and the next condition is evaluated. The process continues until a matching condition is found. If none of the conditions evaluates to true, then blockN, defined just after the else keyword, will be executed. Please note that the last else of the above construct is optional. Also, as a program goes through an if-else-if statement, only one among the defined block of statements will be executed.

public static void main(String[] args) 
{ 
  int temperature = 15 // a value is assigned

  if (temperature > 40) 
  {
    System.out.println("Weather is hot");
  } 
  else if (temperature > 20) 
  {
    System.out.println("Weather is warm");
  } 
  else 
  {
    System.out.println("Weather is cold");
  }    
}

In the above example, because the value 15 is assigned to the temperature variable, the first condition is false, and the first message will not be printed on the console. The execution then jumps to the second condition, whose evaluation returns false as the result; hence, the second System.out.println instruction will also be ignored. The program's execution goes straight to the else, and the message "Weather is cold" is printed on the screen.

Selection Statements in C#

All the above selection statements can be used in C# with similar syntax. The difference is the name of the program entry point, which is Main instead of main, and the use of Console.WriteLine to print messages on the console instead of System.out.println.

static void Main(string[] args) 
{ 
  int temperature = 15 // a value is assigned

  if (temperature > 40) 
  {
    Console.WriteLine("Weather is hot");
  } 
  else if (temperature > 20) 
  {
    Console.WriteLine("Weather is warm");
  } 
  else 
  {
    Console.WriteLine("Weather is cold");
  }    
}

The next article will explain another type of selection statement similar to the if-else-if, known as switch-case.