Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Decision Making Statements

When writing programs, it is very common that we will need to control the flow of execution based on a certain logic. Let us think of simple example for now, a test for equality. Lets say, we want to compare if the two given numbers are equal. The result of the check will either be equal to true or false. If it is true, we wish to perform a set of instructions. If it is false, we might wish to perform a different set of instructions. Lets take a look at the example below.

  package com.example.core;

  public class DecideByIfElse {

    public static void main(String[] args) {
      int intA = 1;
      int intB = 2;
	  //comparing int value using simple if statement
	  if(intA == 1) {
	    System.out.println("The value of intA is " 
	      + "equal to 1");
	  }
	  /*
	   * Since intA is equal to 1, all the lines of codes 
	   * inside the open and close curly brace will get 
	   * executed.
	   */
      //comparing int value using if and else statement
	  if(intB == 1) {
	    System.out.println("The value of intB is " 
	      + "equal to 1");
	  } else {
	    System.out.println("The value of intB is not " 
	      + "equal to 1");
	  }
	  /*
	   * Since intB is not equal to 1, all the lines of 
	   * codes inside the open and curly 
	   * brace for the else condition will get executed.
	   */
	  //comparing int value using if, else if, 
	  //and else statement
	  if(intA == intB) {
        System.out.println("The value of intA is equal to " 
          + "the value of intB.");
	  } else if(intA == 1) {
	    System.out.println("The value of intB is " 
	      + "equal to 1");
	  } else {
	    System.out.println("Nothing matched!");
	  }
	  /*
	   * The if statement will be first be evaluated. 
	   * In this case, it did not satisfy 
	   * the condition since intA is not equal to intB. 
	   * Then, the else if statement will be evaluated.
	   * This time, the condition is satisfied and all 
	   * the lines of codes inside the else if open and  
	   * close curly brace gets executed. The else  
	   * condition will no longer be evaluated since the 
	   * else if condition was already satisfied.
	   */
	  //The principles for all of the above examples can 
	  //be applied for other numeric data types
		
	  String stringA = "ABC";
	  String stringB = "abc";
	  //comparing String value using simple if statement
	  if(stringA.equals("ABC")) {
	    System.out.println("The value of stringA is ABC.");
	  }
	  /*
	   * To compare string values, we use the equals 
	   * String method. This comparison is case sensitive.
	   */
	  if(stringA.equals("ABC")) {
	    System.out.println("The value of stringA and " 
	      + "the value ABC are equal.");
	  }
	  //comparing string values in a non case sensitive way
	  if(stringA.equalsIgnoreCase(stringB)) {
	    System.out.println("The value of stringA and " 
	      + "stringB are equal without case sensitivity.");
	  }
	  //comparing using if and else statements
	  if(stringA.equals(stringB)) {
	    System.out.println("The value of stringA and " 
	      + "stringB are equal.");
	  } else if (stringA.equalsIgnoreCase(stringB)) {
	    System.out.println("The value of stringA and " 
	      + "stringB are equal without case sensitivity.");
	  }
	  /*
	   * As you may have guessed, the block of code under 
	   * the else if condition will get executed.
	   */
    }
  }
                           
Let us now use the switch statement to control the flow of execution.
  package com.example.core;

  /**
   * This will demonstrate how to control the
   * execution flow using the switch statement.
   * 
   * @author Rolan Liwanag
   *
   */
  public class DecideBySwitch {

	public static void main(String[] args) {
	  int varNum = 2;
	  //int and String are the common parameter passed
	  //in a switch statement
	  switch(varNum) {
	    case 1:
          System.out.println("1st place!");
          break;
        case 2:
          System.out.print("2nd");
          System.out.println(" place!");
          break;
        case 3:
          System.out.println("3rd place!");
        default:
          System.out.println("You lost!");
          break;
      }
	  /*
	   * We declared an int variable varNum with a 
	   * value of 2. We passed that value into the
	   * switch statement to be evaluated. It is 
	   * then evaluated if it is equal to 1. Since
	   * it evaluates to false, it is then evaluated
	   * if it is equal to 2. Since the test evaluates
	   * to true, then it executes the 1st print 
	   * command, then executes the second print
	   * command, then executes the break command.
	   * 
	   * NOTES: 
	   * 
	   * The break keyword is responsible for
	   * letting the program exit from the switch 
	   * statement. Without the break command, it
	   * will also execute the print commands from
	   * 'case 3' and 'default'.
       * 
	   * The 'default' will get executed in the
	   * above code if no 'case' condition is met.
	   */
	  String varStr = "abc";
      switch(varStr) {
        case "abb":
          System.out.println("It is abb");
          break;
        case "abc":
          System.out.println("It's abc");
          break;
        default:
          System.out.println("Default only");
          break;
      }
	  /*
	   * The above code shows how to evaluate a String
	   * variable in a switch statement.
	   */
	}
  }
                            
I personally favor the use of if, else if, else statements more than switch statements. It is much flexible since switch statements can only test for equality. You cannot use a switch statement for testing relationships. We will discuss equality and relationship in more detail in the next lesson.


Back    Next