Java Decision Making

 

Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Different types of Decision making.

if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
}

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true, print some text:

Example

if (20 > 18) {
  System.out.println("20 is greater than 18");
}

// 20 is greater than 18

 

int x = 200;
int y = 180;
if (x > y) {
  System.out.println("x is greater than y");
}

// x is greater than y

 

Example explained

In the example above we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 200, and y is 180, and we know that 200 is greater than 180, we print to the screen that "x is greater than y".

 

If Else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
} else { 
  // block of code to be executed if the condition is false
} 

Example

int time = 200;
if (time < 180) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}

// Good evening.

 

Example explained

In the example above, time (200) is greater than 180, so the condition is false, so we move on to the else condition and print to the screen "Good evening". If the time was less than 180, the program would print "Good day".

 

else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example

int time = 22;
if (time < 10) {
  System.out.println("Good morning.");
} else if (time < 20) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Outputs "Good evening."

 

nested if statement

It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.

Syntax

if(Boolean_expression 1) {
   // Executes when the Boolean expression 1 is true
   if(Boolean_expression 2) {
      // Executes when the Boolean expression 2 is true
   }
}

Example

int x = 30;
int y = 10;

if( x == 30 ) {
  if( y == 10 ) {
    System.out.print("X = 30 and Y = 10");
  }
}

 

switch statement

switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax

 

switch(expression) {
   case value :
      // Statements
      break; // optional
   
   case value :
      // Statements
      break; // optional
   
   // You can have any number of case statements.
   default : // Optional
      // Statements
}

The following rules apply to a switch statement −

  • The variable used in a switch statement can only be integers, convertible integers (byte, short, char), strings and enums.

  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

  • The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

  • switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Example

public class Test {

   public static void main(String args[]) {
      // char grade = args[0].charAt(0);
      char grade = 'C';

      switch(grade) {
         case 'A' :
            System.out.println("Excellent!"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

Output

Well done
Your grade is C