Sie sind auf Seite 1von 4

WEEK 5: JUMP STATEMENTS OBJECT – ORIENTED PROGRAMMING

JAVA BREAK STATEMENT


The break statement terminates the loop immediately, and the control of the program moves to the
next statement following the loop.

It is almost always used with decision making statements (if...else Statement).

How break statement works?

Example 1: Java break statement OUTPUT:


1. for (int i = 1; i <= 10; ++i) { 1
2. if (i == 5) { 2
3. break; 3
4. } 4
5. System.out.println(i);
6. }

Example 2: Java break statement

The program below calculates the sum of numbers entered by the user until user enters a negative
number.
WEEK 5: JUMP STATEMENTS OBJECT – ORIENTED PROGRAMMING

In case of nested loops, break terminates the innermost loop.

LABELED BREAK STATEMENT


How labeled break statement works?

Example 3: labeled break Statement

OUTPUT:

i = 1; j = 1
i = 1; j = 2
i = 2; j = 1
WEEK 5: JUMP STATEMENTS OBJECT – ORIENTED PROGRAMMING

JAVA CONTINUE STATEMENT


The continue statement skips the current iteration of a loop (for, while, and do...while loop).

When continue statement is executed, control of the program jumps to the end of the loop. Then, the
test expression that controls the loop is evaluated. In case of for loop, the update statement is executed
before the test expression is evaluated.

It is almost always used with decision making statements (if...else Statement).

How continue statement works?

Example 1: Java continue statement

In case of nested loops, continue skips the current iteration of innermost loop.

LABELED CONTINUE STATEMENT


WEEK 5: JUMP STATEMENTS OBJECT – ORIENTED PROGRAMMING

There is another form of continue statement, labeled form, that can be used to skip the execution of
statement(s) that lies inside the outer loop.

How labeled continue statement works?

Example 3: labeled continue

OUTPUT:

a = 1======== b = 1

a = 1======== b = 2

a = 3======== b = 1

a = 3======== b = 2

a = 4======== b = 1

a = 4======== b = 2

Das könnte Ihnen auch gefallen