Print Page Print Page

CH7 – Break and Continue

  1. Create a new class named BreakContinue.
  2. Add a new method named breakExample. As the class uses the Scanner class, don’t forget to import java.util.Scanner at the top of BreakContinue.
public static void breakExample(){
  Scanner in = new Scanner(System.in);
  while(true){
    System.out.print("Enter a number: ");
    if(in.hasNextDouble()){
      break;
    }
    String word = in.next();
    System.err.println(word + " is not a number.");
  }
  double x = in.nextDouble();
  System.out.println("You entered: " + x);
}
  1. Add main and have it call breakExample.
public static void main(String[] args){
  BreakContinue.breakExample();
}
  1. Compile and run the program.
$ javac BreakContinue.java
$ java BreakContinue
Enter a number: x
x is not a number.
Enter a number: 12
You entered: 12.0

The complete class should appear as follows.

import java.util.Scanner;
public class BreakContinue {
 public static void main(String[] args){
  BreakContinue.breakExample();
 }

 public static void breakExample(){
  Scanner in = new Scanner(System.in);
  while(true){
    System.out.print("Enter a number: ");
    if(in.hasNextDouble()){
      break;
    }
    String word = in.next();
    System.err.println(word + " is not a number.");
  }
  double x = in.nextDouble();
  System.out.println("You entered: " + x);
 }
}
  1. Add a method named continueExample to the class.
public static void continueExample(){
  Scanner in = new Scanner(System.in);
  int x = -1;
  int sum = 0;
  while(x != 0){
    System.out.println("Enter a number: ");
    x = in.nextInt();
    if (x <=0){
      continue;
    }
    System.out.println("Adding " + x);
    sum += x;
  }
  System.out.println("Sum: " + sum);
}
  1. Comment the call to breakExample in main and add a call to continueExample.
public static void main(String[] args){
// BreakContinue.breakExample();
  BreakContinue.continueExample();
}
  1. Compile and run the program.
$ javac BreakContinue.java
$ java BreakContinue
Enter a number: 
1
Adding 1
Enter a number: 
2
Adding 2
Enter a number: 
3
Adding 3
Enter a number: 
0
Sum: 6

The complete class should appear as follows.

import java.util.Scanner;

public class BreakContinue {
 public static void main(String[] args){
  //BreakContinue.breakExample();
  BreakContinue.continueExample();
 }

 public static void breakExample(){
  Scanner in = new Scanner(System.in);
  while(true){
    System.out.print("Enter a number: ");
    if(in.hasNextDouble()){
      break;
    }
    String word = in.next();
    System.err.println(word + " is not a number.");
  }
  double x = in.nextDouble();
  System.out.println("You entered: " + x);
 }

 public static void continueExample(){
  Scanner in = new Scanner(System.in);
  int x = -1;
  int sum = 0;
  while(x != 0){
    System.out.println("Enter a number: ");
    x = in.nextInt();
    if (x <=0){
      continue;
    }
    System.out.println("Adding " + x);
    sum += x;
  }
  System.out.println("Sum: " + sum);
 }
}

LearningLad provides tutorials on both the break and continue statements.