"Hi, Amigo!

"Hi Ellie."

"You've already studied loops, and that's cool."

"I agree! Now I totally don't know how to live without them."

"Sometimes you can do without them... But more often you will need them. Today we're going to talk about the situation when you need to get out of a loop ahead of schedule."

"Do you mean, when the condition for continuing the loop is true, but you still need to exit the loop?"

"Exactly! Sometimes you sound like you're already a programmer. Anyway, to break out of a loop ahead of schedule, you can use the break statement. Take a look at the following example:

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".

"I see. The program reads lines from the console. If you enter "exit", then the isExit variable becomes true, the loop condition !isExit will be false, and the loop will end."

"That's right. So, 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".

"Maybe I'll remember that. I feel that it will come in handy."

"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."

"What do you mean by iteration?"

"A loop iteration is one execution of the loop body. The continue statement interrupts the current iteration of the loop, but unlike the breakstatement, it doesn't end the loop itself. The statement is also brief:

continue;

"So, the continue statement can be used in a loop if we want to 'skip' execution of certain iterations of the loop?"

"Exactly. Do you understand the difference between break and continue? If you need to find a seven among 20 non-repeating digits, what kind of loop and loop interruption statement would you use?

"Hmm... I would go through all the numbers, and if I find 7, then I would execute a break."

"What if you need to display numbers from 1 to 20 except for numbers that are divisible by 7?"

"Well, here I probably need continue, since I don't want to exit the loop. But I still don't quite understand."

"Let me show you how to solve this problem. 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 division by 7 is 0 ), then we skip displaying the number.

"Why are you looking at me so suspiciously, Ellie? Is there some catch here?"

"You can't be fooled, Amigo! Indeed, this code will not work correctly. It will display the first 6 digits, and then i will forever remain stuck on 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. I wrote the code this way on purpose to illustrate a 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);
}

"Excellent! I will try not to make this mistake."

"I'll remember your promise!"