Break and Continue & Switch-Case in Java

Beginners code Computer Skills Java Mobile Development Web Development

Jump Control Statements:

If you haven’t already had an overview of jump control statements in Java, please have a look at it here. In this article, we will start off with the break and continue statement!

Break statement:

  • Break statement transfers control to the outside of the loop, thereby terminating the loop.
  • The program control then resumes at the next statement following the loop.
  • It can be used to terminate a case in the switch statement also.
public class Demo {

  public static void main(String[] args) {
    for (int i = 1; i < 5; i++) {
      System.out.println("Hello" + i);

      if (i == 3) {
        break;
      }
    }
  }
}

Output:

Continue Statement: 

  • It is used to transfer the control to the beginning of the loop.
  • The continue statement is the reverse of the break statement.
  • Instead of terminating or exiting the block, it forces the next iteration of the loop to take place, skipping any code in between.
public class DemoC {
  public static void main(String[] args) {

    for (int i = 1; i <= 5; i++) {
      
      // when i is 3, the control skips print statement
      if (i == 3) {
        continue;
      }

      System.out.println("Hello " + i);
    }
  }
}

Output:

Why Selection Control Statements?

Have a look here, in this article. We will start off with Switch-case here!

Switch Case:

  • It has 4 parts namely:
    1. Switch:
      • It is the control keyword allowing us to make a choice out of multiple options.
    2. Case:
      • This contains the options along with case labels out of which we have to choose our relevant option.
      • The execution starts with the match case.
    3. Break:
      • It is a jump statement which takes the control out of the loop.
      • It prevents the unnecessary flow of control to the subsequent cases after the match case.
    4. Default:
      • The default code block gets executed when none of the cases matches with the case values present in the switch. It is optional.
  • You can define any number of cases inside the switch statement depending upon the requirement.
  • Duplicate cases are not allowed in switch statements.
  • When one option is chosen the statements inside that corresponding case are executed and then control exits the switch statement.
  • This exit is ensured by adding break statements after the case blocks.
  • Syntax:
switch(variable) {

case 1:
  // statements
  break;
case 2:
  // statements
  break;
  .
  .
case n:
  // statements
  break;

default:
  // statements

Have a look at the below programs to understand better the implementation of switch statements.

import java.util.Scanner;

public class MonthsName {

  public static void main(String[] args) {
    
    int month;

    System.out.println("Enter month number from 1 to 12 : ");
    Scanner sc = new Scanner(System.in);
    month = sc.nextInt();
     
    switch(month) {
      case 1:
        System.out.println("January\n");
        break;
      case 2:
        System.out.println( "February\n");
        break;
      case 3:
        System.out.println("March\n");
        break;
      case 4:
        System.out.println("April\n");
        break;
      case 5:
        System.out.println("May\n");
        break;
      case 6:
        System.out.println("June\n");
        break;
      case 7:
        System.out.println("July\n");
        break;
      case 8:
        System.out.println("August\n");
        break;
      case 9:
        System.out.println("September\n");
        break;
      case 10:
        System.out.println("October\n");
        break;
      case 11:
        System.out.println("November\n");
        break;
      case 12:
        System.out.println("December\n");
        break;

      default: 
        System.out.println("You have entered an invalid month number\n");
        break;
      }
  }
}

Output:

Here is another example!

import java.util.Scanner;

public class ArithmaticEquation {
  
  public static void main(String[] args) {
    
    System.out.println("Enter the values of a and b");
    int a;
    int b;
    
    Scanner sc = new Scanner(System.in);
    a = sc.nextInt();
    b = sc.nextInt();
    int result;
    
    System.out.println("Enter the choice \n");
    System.out.println("1. Addition \n 2. Subtraction \n 3. Multiplication");
    int choice = sc.nextInt();
    
    switch (choice) {
    case 1:
      System.out.println("You chose Addition");
      result = a + b;
      System.out.println("Result is: " + result);			
      break;
    
    case 2:
      System.out.println("You chose Subtraction");
      result = a - b;
      System.out.println("Result is: + result);			
      break;
    
    case 3:
      System.out.println("You chose Multiplication");
      result = a * b;
      System.out.println("Result is: " + result);			
      break;

    default: 
      System.out.println("You have entered an invalid choice! Try again!");
      break;
    }
  }
}

Output:

 

Why is the break statement necessary after each case?

  • Writing a break statement is optional. But it becomes necessary if you want to execute only the match option.
  • If there is no break statement, all consecutive blocks of codes present after the match case will be executed.
  • The break statement at the end of each case causes the switch statement to exit.
  • As a result, only the match option will be executed.

Is it necessary to use default case in switch case?

  • In most of the cases, switch statements have a default case to ‘catch’ an unexpected value.
  • It is a type of exception handling for switch statements.
  • If you enter an invalid option, then instead of throwing an error or stopping the program, the control moves to the default block.
  • Thus if there is no match with any case label, the code block associated with the default keyword is executed.

In the next article, we will learn about the arrays and how to operate on them. The article will also contain a brief introduction to the String class available in Java. The article will cover basic array creation, operations on array elements, string methods, string operations.