Decision Making in Java – If, If Else, Switch, Break, Continue

HOME
  

Decision making in Java is same as real life decision making. I remember during my childhood, my father had told me once that if I get 1st rank in class will get a bicycle, else if 2nd will get a walk man, else if 3rd video game else nothing. This is an ideal example of if-else scenario. If a particular condition is true, then execute the specified block of code. In programming language too, we execute certain lines of code if the condition is satisfied. In programming language too, we follow the similar concept, such as

• Block of code to be executed, if a specified condition is true
• Use else to specify a block of code to be executed, if the same condition is false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed

The if condition 

if (condition) {
    // block of code to be executed if the condition is true
public class Java_Demo {
    public static void main(String[] args) {
        int x=50;
        int y=10;
       
	    if(x>y)
       {
            System.out.println("x is greater than y");
       }
    }
}

Output
x is greater than y

 If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition ) then by default if statement will consider the immediate one statement to be inside its block. For example,

if(condition)
     statement1;
     statement2;

  // Here if the condition is true, if block will consider only statement1 to be inside its block.

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

The else Statement

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
   } 
 public class Java_Demo {
     public static void main(String[] args) {
         int x=5;
         int y=10;
         if(x>y)
            {
                System.out.println("x is greater than y");
            }
          else
            {
                System.out.println("y is greater than x");
            }
        }
    }

Output
y is greater than x

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

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
   }
public class Java_Demo {

	public static void main(String[] args) {
		int x = 10;
		int y = 10;
		if (x > y) {
			System.out.println("x is greater than y");
		} else if (y > x) {
			System.out.println("y is greater than x");
		} else {
			System.out.println("x is equal to y");
		}
	}
}

Output
x is equal to y

Switch Statement

  • The switch expression is evaluate once.
  • The value of the expression is compare with the values of each case.
  • If there is a match, the associated block of code is execute.
  • The break and default keywords are optional

    Use the switch statement to select one of many code blocks to execute. It is use to replace multilevel if-else-if statement. 
However, the switch statement can be use only if the conditions based on the same constant value.

  switch(expression) {
       case x:
      // code block
       break;
       case y:
     // code block
       break;
      default:
      // code block
   }

 The default statement is use as the last statement in a switch block; it does not need a break.

 When Java reaches a break keyword, it breaks out of the switch block.

public class Switch_Statement {
    public static void main(String[] args) {
		int month_no = 3;
		switch (month_no) {
		case 1:
			System.out.println("January");
			break;
		case 2:
			System.out.println("February");
			break;
		case 3:
			System.out.println("March");
			break;
		case 4:
			System.out.println("April");
			break;
		case 5:
			System.out.println("May");
			break;
		case 6:
			System.out.println("June");
			break;
		default:
			System.out.println("Rest of months");
		}
	}
}

Output
March

Default Keyword

The default keyword specifies that specific code will run if there is no case match.

public class Default_Demo {

	public static void main(String[] args) {
		int month_no = 9;
		switch (month_no) {
		case 1:
			System.out.println("January");
			break;
		case 2:
			System.out.println("February");
			break;
		case 3:
			System.out.println("March");
			break;
		case 4:
			System.out.println("April");
			break;
		case 5:
			System.out.println("May");
			break;
		case 6:
			System.out.println("June");
			break;
		default:
			System.out.println("Rest of months");
		}
	}
}

Output
Rest of months

Break Statement

 It terminates the loop by skipping the execution of any remaining code

public static void main(String[] args) {
    for(int i=0;i<5;i++)
      {
         if(i==2)
         break;
         System.out.println("Value of i is: "+i);
       }
         System.out.println("Loop finishes");
     }
   }

Output
Value of i is: 0
Value of i is: 1
Loop finishes

Continue Statement


It continue running the loop, just skip a particular iteration of the loop.

public class Continue_Demo {
   public static void main(String[] args) {
        for(int i=0;i<5;i++)
         {
             if(i==2)
             continue;
             System.out.println("Value of i is: "+i);
           }
         System.out.println("Loop finishes");
     }
  }

Output
Value of i is: 0
Value of i is: 1
Value of i is: 3
Value of i is: 4
Loop finishes

2 thoughts on “Decision Making in Java – If, If Else, Switch, Break, Continue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s