If, if-else, if if-else else, switch - Decision making statements with examples in java


Decision making statements in java >
    • 1.1) if
    • 1.2) if-else
    • 1.3) if if-else else
    • 1.4) switch

Decision making statements are Control Flow Statements. Control Flow Statements are statements inside your source files which are generally executed from top to bottom, in the order that they appear.


Now, let’s discuss different decision making statements in java in detail >
    • 1.1) if
    • 1.2) if-else
    • 1.3) if if-else else
    • 1.4) switch


1.1) If statements

Syntax of if statement>


            if (booleanVal) {
                 // Statements will execute if the booleanVal is true
          }

If booleanVal evaluates to true then the statements inside if block are executed.
More about if statement >
  • If only one statement is used inside if then enclosing those statements in brackets is optional.

             if(booleanVal)
                   //Only a Statement..

  • Or, if we look at it another way - If statements inside if are not enclosed in bracket then only first statement is considered inside if.

             if(booleanVal)
                   //Statement1. (Will be inside if)
                   //Statement2. (Will be outside if)



Program 1.1 to demonstrate if statement
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          int marks = 50;
         
          if(marks>=40){
                 System.out.println(">=40 - PASS");
          }         
   }
}
/* OUTPUT
>=40 - PASS
*/

1.2) if-else

Syntax of if-else statement >


             if (booleanVal) {
                 // Statements will execute if the booleanVal is true
          } else {
                 // Statements will execute if the booleanVal is false
          }



if-else statement rules >
If booleanVal evaluates to true then the statements inside if block are executed and statement inside else are not executed.
Program 1.2 to demonstrate if-else statement
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          int marks = 39;
         
          if(marks>=40){
                 System.out.println(">=40 - PASS");
          }
          else{
                 System.out.println("<40 - FAIL");
          }
   }
}
/* OUTPUT
<40 - FAIL
*/


1.3) if if-else else

Syntax of if if-else else statement>


            if (booleanVal1) {
                 // Statements will execute if the booleanVal1 is true
          } else if (booleanVal2) {
                 // Statements will execute if the booleanVal is true
          } else if (booleanVal3) {
                 // Statements will execute if the booleanVal3 is true
          }
          // Also java allows us to use any number of if-else conditions.
          else {
                 // Statements will execute if all of the above booleanVal are false.
          }


if if-else else statement rules >
  • If booleanVal evaluates to true in any if or if-else then >
the remaining if-else condition are not evaluated (if any), and
else block is not executed.
  • Also java allows us to use any number of if-else conditions.

Program 1.3 to demonstrate if if-else else statement>
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          int marks = 61;
          if(marks>=80){
                 System.out.println(">=80 - Grade A");
          }
          else if(marks>=60){
                 System.out.println(">=60 - Grade B");
          }
          else if(marks>=40){
                 System.out.println(">=40 - Grade C");
          }
          else{
                 System.out.println("<40 - FAIL");
          }
   }
}
/* OUTPUT
>=60 - Grade B
*/

1.4) switch

Syntax of switch statement>


             switch (expression) {
                 case value:
                       // Statements..
                       break; // optional
  
                 case value:
                       // Statements..
                       break; // optional
  
                 // Java allows us to use any number of case statements.
  
                 default: // Optional default statements will execute when none of the
                                     // above case value matches
                       // Statements..
                
          }

switch statement rules >
  1. Before java 7 > byte, short, int, or char can only be used as expression in switch statement (i.e. all those data types which could be implicitly casted into int - read Implicit casting/promotion of primitive Data type).
  2. Java 7 allows to use String as expression in switch statement

  1. Java allows us to use any number of case.

  1. The data type of value of case must have same data type as of expression.

  1. Case is executed - when expression is equal to value in case.

  1. Optional break statement >
    • If break statement is not used (bad practice) >
      • all the statements in below cases will be executed,
      • including statements in default.
    • If break statement is used >
      • switch terminates and
      • flow of control reaches right to the next line of switch.

  1. Optional default >
    • Optional default statements will execute when none of the above case value matches
    • Using break in default doesn’t matter, as it is last case in switch.

Program 1.4.1 to demonstrate switch statement
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          char grade = 'B';
          switch (grade) {
                 case 'A':
                       System.out.println("Grade A - marks >=80");
                       break;
  
                 case 'B':
                       System.out.println("Grade B - marks >=60");
                       break;
                 case 'C':
                       System.out.println("Grade C - marks >=40");
                       break;
                      
                 case 'F':
                       System.out.println("Grade F - marks <40 FAIL");
                       break;
                      
                 default : //optional
                       System.out.println("Invalid Grade");
                       break; //optional
          }
   }
}
/*OUTPUT
Grade B - marks >=60
*/


Program 1.4.2 -  Java 7 allows to use String as expression in switch statement
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          String marks="marks >=60";
          switch (marks) {
                 case "marks >=80":
                       System.out.println("Grade A");
                       break;
  
                 case "marks >=60":
                       System.out.println("Grade B");
                       break;
                 case "marks >=40":
                       System.out.println("Grade C");
                       break;
                      
                 case "marks <40 FAIL":
                       System.out.println("Grade F");
                       break;
                      
                 default : //optional
                       System.out.println("Invalid marks");
                       break; //optional
          }
   }
}
/*OUTPUT
Grade B
*/


Java allows us to use nested Decision making statements
    • nested if
    • nested if-else
    • nested if if-else else
    • nested switch

eEdit
Must read for you :