Breaking News

The continue statement | Decision making, Branching and Looping

The continue statement

1) Can only appear within a loop.
2) Skips all remaining statements within the loop and resumes with the next iteration.

For example,
for (int x = 0; x < 10; x++)
{
if (x == 5)
continue;
else
System.out.print(” ” + x);
}

will short-circuit when x takes on a value of 5.

The output displayed will be:
0 1 2 3 4 6 7 8 9

3)It may specify the label of the loop to be continued. This makes it possible to skip to the next iteration of the specified loop.

For example,
outer: for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= 1; j–)
{
if (i == j)
continue outer;
else
System.out.println(“i = ” + i + “, j = ” + j);
}
}

will short-circuit to the next iteration of the loop labeled outer when i equals j.

The output displayed will be:
i = 1, j = 5
i = 1, j = 4
i = 1, j = 3
i = 1, j = 2
i = 2, j = 5
i = 2, j = 4
i = 2, j = 3
i = 3, j = 5
i = 3, j = 4
i = 4, j = 5

 

Check Also

Casts

1. Are a programmer’s way of telling the compiler to ignore possible loss of data …