Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 11: Using break and continue

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala


Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

The Java Lesson 11

Using break and continue


Overview

At times, it is desirable to exit from a loop or short-circuit the current iteration of a loop. The break and continue statements make it happen.

The break statement

  • Can only appear within a loop or a switch statement.

  • Causes an immediate exit from a loop structure before the test condition is met. Once a break statement is encountered, the loop immediately terminates, skipping any remaining code. For example,

for (int x = 0; x < 10; x++) {
if (x == 5)
break;
else
System.out.print(" " + x);
}

will end prematurely when x takes on a value of 5. The output displayed will be:

0 1 2 3 4

  • May specify the label of the loop to be abandoned. This makes it possible to abandon a specific loop. For example,

    outer: for (int i = 1; i <= 5; i++) {
    for (int j = 5; j >= 1; j--) {
    if (i == j)
    break outer;
    else
    System.out.println("i = " + i + ", j = " + j);
    }
    }

    will end the outer loop 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

  • Causes an immediate exit from a switch, skipping any remaining code. For example, if key is a char variable having a value entered by the user, the following statements will display whether they entered an 'A' or a 'B':

switch (key) {
case 'a':
case 'A':
System.out.println("You entered an 'A'");
break;
case 'b':
case 'B':
System.out.println("You entered a 'B'");
break;
default:
System.out.println("You did not enter an 'A' or a 'B'");
break;
}

The continue statement

  • Can only appear within a loop.

  • 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

  • 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

Review questions

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

int x = 7;
if (x > 6) {
break;
System.out.println("Big number");
}
else {
System.out.println("Small number");
}

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. the statements will compile but nothing will be displayed

  4. the statements will compile and "Big number" will be displayed

  5. the statements will compile and "Small number" will be displayed

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

while (true) {
for (int i = 3; i > 0; i--) {
if (i == 3)
continue;
else
System.out.println(i);
}
break;
}
System.out.println("All done");

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. 2
    1
    2
    1
    (in an endless loop)

  4. 2
    1
    All done

  5. All done

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

for (int i = 1; i <= 3; i++) {
for (int j = 3; j >= 1; j--) {
if (i != j)
continue;
else
System.out.println("i = " + i + ", j = " + j);
}
}

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. i = 1, j = 1
    i = 2, j = 2
    i = 3, j = 3

  4. i = 3, j = 3

  5. the statements will compile but nothing will display

  1. Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?

for (int i = 1; i <= 3; i++) {
for (int j = 3; j >= 1; j--) {
if (i != j)
break;
else
System.out.println("i = " + i + ", j = " + j);
}
}

  1. the statements will not compile

  2. the statements will compile but a run time error will occur

  3. i = 1, j = 1
    i = 2, j = 2
    i = 3, j = 3

  4. i = 3, j = 3

  5. the statements will compile but nothing will display


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy