Decision-Making in Java With Examples

In programming, we often branch our code and decide to follow a set of instructions on the basis of certain conditions being met. This is called decision making. A java expression is evaluated and on the basis of its boolean return, the configured set of instructions is performed.

Example:

In the real world, we often come across a situation where we are uncertain of the outcome, and we decide a distinct set of actions taking into account all possible outcomes. If I reach home and find food being available, I will eat that else I will order a pizza.

Java has a variety of decision-making approaches.

  • if statement
  • if-else statement
  • if –else-if ladder
  • nested if statement
  • switch block

Let us look into each of the variations in detail.

If statement

The java-expression is evaluated, and if comes out as true, then the set of instructions enclosed in the third brackets will be executed.

Syntax:

if(java-expression)
{
   //Set of instructions for if block                                                              
}

Example:

public class MyDecisions{
     public static void main(String []args){
        int myAge = 67;
        System.out.println("Before if block");

        if(myAge>60) // evaluation is true
        {
            System.out.println("Senior Citizen");
        }

        System.out.println("After if block");

        System.out.println("\n\nBefore 2nd if block");

        if(myAge<18) // evaluation is false
        {
           System.out.println("Minor");
        }
        System.out.println("After 2nd if block");
     }
}

Output:

Before if block
Senior Citizen
After if block

Before 2nd if block
After 2nd if block

If-else  statement

The java-expression is evaluated, and if comes out as true, then the set of instructions enclosed in if block is executed, if it comes out as false, then the set of instructions enclosed in else block is executed.

Syntax:

if(java-expression)   
{                      
	//Set of instructions for if block                                                   
}
else
{                    
	//Set of instructions for else block                                                            
}     

Example:

public class MyDecisions{
     public static void main(String []args){

        int myAge = 67;
        System.out.println("Before if block");

        if(myAge>60) // evaluation is true
        {
            System.out.println("Senior Citizen");
        }
        else
        {
            System.out.println("Not a Senior Citizen");
        }

        System.out.println("After if block");       

        myAge=22; //myAge is set to 22 now       

        System.out.println("\n\nBefore 2nd if block");

        if(myAge>60)
        {
            System.out.println("Senior Citizen");
        }
        else
        {
            System.out.println("Not a Senior Citizen");
        }
        System.out.println("After 2nd if block");
    }
}

Output:

Before if block
Senior Citizen
After if block

Before 2nd if block
Not a Senior Citizen
After 2nd if block

If-else-if  ladder

In case we have a series of java expressions to be evaluated in a sequence, provided the preceding expression was evaluated as false, we use the if-else-if ladder. The else at the very end is optional as we saw in our previous versions.

Syntax:

if(java-expression1)
{
	//Set of instructions  for if block
}
else if(java-expression2)
{
	//Set of instructions  for else-if block 1
} 
else if(java-expression3)
{
	//Set of instructions  for else-if block2 and so on
} 
else 
{
} 

 Example:

public class MyDecisions{
     public static void main(String []args){
        int myAge = 44;
        System.out.println("Before if-else-if ladder");

        if(myAge<=12)
        {
            System.out.println("Juvenile");
        }
        else if(myAge>=12 && myAge<20)
        {
            System.out.println("Teen");
        }
        else if(myAge>=20 && myAge<60)
        {
            System.out.println("Adult");
        }
        else if(myAge>=60)
        {
            System.out.println("Senior Citizen");
        }
        System.out.println("After if-else-if ladder");
    }
}

Output:

Before if-else-if ladder
Adult
After if-else-if ladder

Nested if  block

We can have if block inside another if block, which is called a nested-if block. The entry point of the inner-if block is dependent on the outer if block to be true

Syntax:

if(java-expression1)
{                                                                                          
   if(java-expression2)
	{  
		//Set of instructions	
    }           
}

Example:

public class MyDecisions{
     public static void main(String []args){
        int myAge = 17;
        System.out.println("Before nested if statement");

        if(myAge>12)
        {
            if(myAge<20)
            {
                System.out.println("Your age is more than 12 and less than 20");
            }
        }
         System.out.println("After nested if statement");
   }
}

Output:

Before nested if statement
Your age is more than 12 and less than 20
After nested if statement

Switch block

If we have a variable, that can have one value out of N number of possible outcomes. We go for the switch statement. It is similar to the if-else-if ladder. The possible outcomes are mentioned with keyword case to be checked, which upon match executes the configured set of instructions. The variable can be of any primitive data type or Wrapper class objects. In case, none of the case value matches, the default block is executed.

Syntax:

switch(expression) {
   case option1: 
		// set of instructions break;
   case option2: 
		// set of instructions break;
   case option3: 
		// set of instructions break;
   default:      
		//set of instructions
}

Example:

public class MyDecisions{
     public static void main(String []args){
        int myAge = 14;
        System.out.println("Before switch statement");
        switch(myAge)
        {
            case 10: System.out.println("Your age is 10"); break;
            case 11: System.out.println("Your age is 11"); break;
            case 12: System.out.println("Your age is 12"); break;
            case 13: System.out.println("Your age is 13"); break;
            case 14: System.out.println("Your age is 14"); break;
            case 16: System.out.println("Your age is 16"); break;
            case 17: System.out.println("Your age is 17"); break;
            default: System.out.println("None of the cases matched");
        }
        System.out.println("After switch statement");
   }
}

Output:

Before switch statement
Your age is 14
After switch statement

This was an overview of Java Decision-making. The next lecture will be about Java loops.