The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Decision Statements in Java

Using decision statements, we can make our programs execute certain statements depending on some conditions. Next we illustrate the decision statements supported by Java.

' if ' statement
It consist of a boolean expression and we execute the set of statements in the ' if ' block depending on the value (True/False) of the boolean expression. Following program demonstrates the use of ' if ' statement :

public class IfStatement {
    public static void main(String args[]) {
      int num = 67;
      System.out.println("Number is " + num);
      // check the condition
      if ( num < 100 ) { // if block starts here
         // condition is true, so print the following
         System.out.println("No. is less than 100 ");
      } // if block ends here
    }
}


' if ... else ' statement
This statement is used when there are multiple conditions depending on which different statements are to be executed. See the following program to understand the use of ' if ... else ' construct :

public class Greater {
    public static void main(String args[]) {
      int x = 7, y = 5;
      if ( x > y ) { // if block starts
         // condition is true, so print the following
         System.out.println("The greater number is " + x);
      } // if block ends
      else { // else block starts
         System.out.println("The greater number is " + y);
      } // else block ends
    }
}

Following program shows a chain of if ... else statements :

/*
 * This program assigns grade to students depending on marks obtained
 * Marks Obtained     Grade
 *     >= 90            A
 * >= 80 and < 90       B
 * >= 70 and < 80       C
 * >= 60 and < 70       D
 * >= 50 and < 60       E
 *     < 50             F
 */
public class Grade {
    public static void main(String args[]) {
      int marks = 72; // you can this value and run the program
      char grade = 'N'; // No grade assigned yet
      // Assign the grade depending on marks
      if ( marks >= 90 ) {
         grade = 'A';
      }
      else if ( marks >=80 && marks < 90 ) {
          grade = 'B';
      }
      else if ( marks >=70 && marks < 80 ) {
          grade = 'C';
      }
      else if ( marks >=60 && marks < 70 ) {
          grade = 'D';
      }
      else if ( marks >=50 && marks < 60 ) {
          grade = 'E';
      }
      else {
          grade = 'F'; // Failed
      }
      System.out.println("Grade assigned is " + grade);
    }
}


' switch ' statement
switch statement is used when a condition or a variable has to be tested for equality against multiple values. Following program illustrates the use of switch construct :

/*
 * This program gives to students depending on their grades
 * Grade   Comments
 *   A     Outstanding
 *   B     Excellent
 *   C     Very Good
 *   D     Average
 *   E     Below Average
 *   F     Fail
 */
public class GradeComment {
    public static void main(String args[]) {
      char grade = 'D'; // grade can be changed and run
      // Print comment depending on grade
      switch (grade) { // switch block starts here
          case 'A' : System.out.println("Outstanding");
                      break; // break out of switch block
          case 'B' : System.out.println("Excellent");
                      break;
          case 'C' : System.out.println("Very Good");
                      break;
          case 'D' : System.out.println("Average");
                      break;
          case 'E' : System.out.println("Below Average");
                      break;
          case 'F' : System.out.println("Fail");
                      break;
          default  : System.out.println("Invalid Grade");
                      break;
      }
    }
}


Nesting of Decision statements
We can use a decision statement inside the block of another decision statement. This is known as nesting. See the following program to understand nesting :

/*
 * This program calculates the bonus for an employee depending
 * on the following conditions :-
 * 1) Employee should be regular
 * 2) Bonus = (No. of years served * 1 month salary) if years served <= 3
 * 3) Bonus = 3 months salary if years served > 3
 */
public class Bonus {
    public static void main(String args[]) {
      /* R -> regular employee
         change it to other value for non-regular employee */
      char emp_type = 'R';
      if ( emp_type == 'R' ) { // regular employee
         float bonus = 0.0f;
         float salary = 37161.53f;   // change it for new bonus
          int years_served = 2;      // change it to see different bonus
          if ( years_served <= 3 ) {
               bonus = salary * years_served;
          }
          else { // non-regular employee
               bonus = salary * 3;
          }
          System.out.println("Bonus : " + bonus);
      }
      else { // Non-regular employee
          System.out.println("Not eligible for bonus");
      }
    }
}

Back | Next