Sei sulla pagina 1di 5


Break and continue in Java are two essential keyword beginners needs to familiar
while using loops ( for loop, while loop and do while loop). break statement in java is used to
break the loop and transfers control to the line immediate outside of loop while continue is
used to escape current execution and transfers control back to start of the loop. Both break
and continue allows programmer to create sophisticated algorithm and looping constructs. In
this java tutorial we will see example of break and continue statement in Java and some
important points related to breaking the loop using label and break statement. break keyword
can also be used inside switch statement to break current choice and if not used it can cause
fall-through on switch. Well break is not alone on breaking switch case you can also sued
throw keyword on switch case.
Break and continue example in java for loop. In this example we are calculating sum
of odd numbers until number 5 appear, where we break the loop by using break statement.
after calculating sum we also call continue which cause last line of loop to not execute.
remember call to continue is inside if block which checks for odd numbers, so that line will
appear in case of even number but not in case of odd number. This example of break and
continue shows that upon calling break, loop terminates and control goes to first line outside
the loop while upon calling continue, loop doesn't terminate but rest of execution doesn't
happen and loop continues from next iteration.
You can use labels with break and continue. labels are where you can shift control
from break and continue. by default break goes outside of loop but if you more than one loop
you can use break statement to break a specific loop by providing label. same is true for
continue. if you are working on nested loops labels gives more control to break and continue.
In following example of break and continue with labels. we have a nested loop and we have
created two labels OUTER and INNER. OUTER label is for outer loop and INNER label is
for inner loop. we are iterating through array and print value of odd number from outer loop
and then use continue statement, which ignores execution of inner loop. if its even number
than inner loop executes and breaks as soon as it prints the number.


Chaining the cases without break; was not sufficient since it only allows jump from
one case to one imediatelly following and to jump from multiple to one. StackOverflow,
which mentioned combination of switch inside while and use continue to repeat switch
match. This idea put me on track. I have slightly improved the examples and created pretty
simple and fully working implementation of goto case ... for Java. It goes like this:
// we need variable for switch condition
int jumpTo = someInitialValue;
do {
switch(jumpTo) {
case 1:
// do some action and continue to case 2 without break
case 2:
// do more action and exit with break
// optionally do default action or
// throw Exception() if you want
// to pass only defined cases
// if the switch successfully completes,
// it breaks while loop here
} while (true);

If you inside switch reassign jumpTo variable to new target case and then call
continue;, the switch will run in the loop again and will match the targeted case. The
while(true); statement allows infinite iteration, so you can call continue; as many times you
need. When your case is final, you end it with break; as usual. This exits switch and right
after the switch is another break; which terminates while loop.
A default statement is useful when you need to select one of several alternatives based
on the value of an integer, a character, or a String variable. The basic form of the switch
statement is this:
switch (expression)
case constant:
[ case constant-2:
break; ] ...
[ default:
break; ] ...
The expression must evaluate to an int, short, byte, or char. It cant be a long or a
floating-point type. Switch. This is a selection statement. It acts upon a variable and selects
the case-statement that matches. Switch has limits, but has also advantages and elegance. Fall
through. In a switch, control flow falls through to the next case. We must be careful to use
breaks (or returns) to prevent incorrect behavior.Case This example loops over the numbers 0,
1 and 2. The switch has no case for 0, so the default block is reached. For 1 the first case

Java program that uses switch

public class Program {
public static void main(String[] args) {
// Loop through 0, 1, and 2.
for (int i = 0; i <= 2; i++) {
// Switch on number.
switch (i) {
case 1: {
System.out.println("One: " + i);
default: {
System.out.println("Default case: " + i);

Default case: 0
One: 1
Two or three: 2
Default case: 2

Each grouping of code lines that starts with the case keyword and ends with a break
statement is a case group. You can code as many case groups as you want or need. Each
group begins with the word case, followed by a constant (usually, a numeric, character, or
string literal) and a colon.Then you code one or more statements that you want executed if
the value of the switch expression equals the constant. The last line of each case group is a
break statement, which causes the entire switch statement to end. The default group, which is
optional, is like a catch-all case group. Its statements are executed only if none of the
previous case constants matches the switch expression.