Loops (break and return, continue, labels) - 1

"Hi, Amigo!"

"Today I will tell you about some things that are convenient when working with loops."

"The first is the keyword break. If you use this command in the body of a loop, then the loop will terminate immediately when the command is executed. Here's an example:"

Example Output:
for (int i = 0; i < 10; i++)
{
System.out.println(i);
if (i > 5)
break;
}
0
1
2
3
4
5

"Can break only be used in a loop?"

"Yes. The break statement can only be used in a loop. When a break statement is executed, the loop terminates immediately."

"OK. Got it."

"Great. Now the second thing I want to share is the keyword continue. It can also only be used in a loop. When this command is executed, a new iteration of the loop begins immediately. In other words, any remaining code in the body of the loop is simply skipped."

"Here's an example:"

Example Output:
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
continue;
System.out.println(i);
}
1
3
5
7
9

"So, once the program reaches a continue command in a loop, it stops executing the code in the loop?"

"No. Look, we have a loop when we execute the same code several times. In the example above, we have a loop from 0 to 9, i.e. the body of the loop will be executed 10 times. Right?"

"Yes."

"One pass through the body of the loop is called an iteration. Our loop consists of 10 iterations—the body of the loop will be executed ten times."

"Yes, that's clear."

"The continue command terminates the current iteration prematurely, i.e. any remaining code inside the loop is skipped and a new iteration begins."

"Here's another example:"

Example
ArrayList list = new ArrayList();
for (Object o: list)
{
if (o == null) continue;
System.out.println(o.toString());
}

"In this example, we display a string representation of all the objects in list. But we skip any objects that are null."

"OK, got it. I can see how convenient this is."

"Yep. I also want to tell you about labels. They are rarely used in Java, because they often violate the beauty of a program's logic. But you may encounter them in the code at some point. So I'd rather tell you about them than have you hear about them on the playground."

"Once upon a time it was permissible to jump from any line to any line in code. We did this using labels and the goto statement. Here's how it looks:"

Horrible code with labels
System.out.println("Make cookies,");
label: System.out.println("not");
System.out.println("war");
goto label;

"In this example, after the goto label command is executed, the program jumps to the line marked label.

"Later, everyone wised up and decided to not use the goto statement. Java doesn't support goto, but goto is a reserved word. Not a big deal…"

"So, no goto and no labels in Java?"

"There is no goto statement, but there are labels!"

"How can that be?"

"In Java, labels can be used with the continue and break commands. They are used when you have many nested loops."

"For example, suppose you have 5 nested loops and when some condition is satisfied you want to get out of three of them, but not all of them. Labels are an elegant way to do this:"

Example
label1: for (int i = 0; i < 10; i++)
 label2: for (int j = 0; j < 10; j++)
  label3: for (int k =0; k < 10; k++)
   if (i == j && j == k)
    break label2;

"In this example, when the break statement is executed, we exit not from the loop with variable k, but from the loop labeled label2 - i.e. we immediately exit the two loops with k and j."

"How often is that used?"

"To be honest, not often, but you never know. Maybe you'll see it sometime. These are syntax fundamentals—you need to know all this!"

"OK. Thank you, Bilaabo."