CodeGym /Courses /Java Syntax Zero /Interrupting a loop

Interrupting a loop

Java Syntax Zero
Level 4 , Lesson 4
Available

1. break statement

Let's take a look at an example from the previous lesson:

Code Explanation
Scanner console = new Scanner(System.in);
boolean isExit = false;
while (!isExit)
{
   String s = console.nextLine();
   isExit = s.equals("exit");
}
The program will read a line from the keyboard, until you enter "exit".

The program reads lines from the console until the word exit is entered. If you enter this word, then the isExit variable becomes true, the loop condition !isExit will be false, and the loop will end."

Java has a special break statement that lets you simplify such logic. If a break statement is executed inside a loop, then the loop ends immediately. The program will start executing the statement that follows the loop. The statement is very brief:

break;

Here's how you can use the break statement to rewrite the example we just discussed:

Code Explanation
Scanner console = new Scanner(System.in);
while (true)
{
   String s = console.nextLine();
   if (s.equals("exit"))
     break;
}
The program will read a line from the keyboard, until you enter "exit".

4
Task
New Java Syntax, level 4, lesson 4
Locked
10 numbers
Display the numbers from 1 to 10 using a while loop. Each value should be on a new line.

2. continue statement

But break isn't the only Java statement that lets you control a loop's behavior. Java also has the continue statement. If you execute a continue statement inside a loop, the current iteration of the loop will end ahead of schedule.

Executing the loop body once is called an iteration of the loop. The continue statement interrupts the current iteration of the loop, but unlike the break statement, it doesn't end the loop itself. The statement is also brief:

continue;

The continue statement is super convenient in a loop if we want to 'skip' execution of the loop body in certain situations.

Task: We want to write a program that prints numbers from 1 to 20 but skips numbers that are divisible by 7. This is what this code might look like.

Code Explanation
int i = 1;
while (i <= 20)
{
   if ( (i % 7) == 0 )
     continue;
   System.out.println(i);
   i++;
}
The program displays numbers from 1 to 20. If the number is divisible by 7 (the remainder of division by 7 is 0), then we skip displaying the number.

Actually, this code will not work, because i will be forever stuck at the number 7. After all, the continue statement skips two other statements: System.out.println(i) and i++. As a result, once we reach the value 7, the variable i will stop changing and we'll be in an infinite loop.

We wrote the code this way on purpose to illustrate this very common mistake. How do we fix it?

There are two options here:

Option 1: change i before executing continue, but after i % 7

Option 2: always increment i at the beginning of the loop. But then i's starting value must be 0.

Option 1 Option 2
int i = 1;
while (i <= 20)
{
   if ( (i % 7) == 0 )
   {
     i++;
     continue;
   }
   
   System.out.println(i);
   i++;
}
int i = 0;
while (i < 20)
{
   i++;
   if ( (i % 7) == 0)
     continue;
   System.out.println(i);
}

4
Task
New Java Syntax, level 4, lesson 4
Locked
From 10 to 1
Display the numbers from 10 to 1 using a while loop. Each value should be on a new line.
4
Task
New Java Syntax, level 4, lesson 4
Locked
You can't have too much of a good thing
Use the keyboard to enter a string and a number N greater than 0. Use a while loop to display the string N times. Each value should be on a new line. Example input: abc 2 Example output: abc abc
4
Task
New Java Syntax, level 4, lesson 4
Locked
Seeing dollars in your future
Use a while loop to display a 10x10 square of dollar signs. Don't separate the symbols in each line. Example output: $$$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$
Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Knightrider Level 12, London, United Kingdom
22 December 2024
int count = 1; int sum = 0; while(count <= 100){ if(count % 3 == 0){ count++; continue; } sum += count; count++; } System.out.println(sum);
RdeRuijter Level 4, Netherlands Expert
11 February 2024
My code gives the exact outcome, using the while loop and continue statement. Still it's not getting passed. Frustrating.
haalk3n Level 6, Romania
2 February 2024
The way they word their problems are breaking me.
Abdulaziz alshaikh Level 1, Saudi Arabia
8 February 2023
Honestly i don't the mathematical task i just download the solution
Devendra Gowd Level 6, Anantapur, India
20 December 2022
whats that last task.. its solution is sum of not multiples of 3, but output is showing wrong multiples. And correct sum of multiples of 3 * is 3468. but what im not understanding ..what this code is it wrong?!
Anonymous #11234130 Level 2, Finland
29 December 2022
I think you are calculating the 101 at the end so your loop goes one step too far. Correct answer is 3367.
Sharron Heath Level 47, Selah, United States
26 May 2022
You tested us on the use of break and continue a while back before teaching it here.