concept continue in category java

appears as: continue
Java SE 11 Programmer I Certification Guide MEAP V03

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide MEAP V03.

  • Using break and continue statements
  • As soon as a loop encounters continue, it exits the current iteration of the loop. In this example, it skips the printing step for the array value Shreya. Unlike the break statement, continue doesn’t exit the loop—it restarts with the next loop iteration, printing the remaining array values (that is, Selvan and Harry).

    When you use the continue statement with nested loops, it exits the current iteration of the inner loop.

    Figure 6.23 compares how the control transfers out of the loop and to the next iteration when break and continue statements are used.

    Figure 6.23 Comparing the flow of control when using break and continue statements in a loop

    For the first for iteration, the variable i has a value of 0. Because this value is less than 2, the following if construct evaluates to true and the continue statement executes:

    if (i < 5) continue;

    Because the continue statement ignores all the remaining statements in a for loop iteration, the control doesn’t print the value of the variable i, which leads the control to move on to the next for iteration. In the next for iteration, the value of the variable i is 5. The for loop condition evaluates to false and the control moves out of the for loop. After the for loop, the code prints out the value of the variable i, which increments once using the code i=i+5.

    OCA Java SE 8 Programmer I Certification Guide

    This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

  • Using break and continue statements
  • For the first for iteration, the variable i has a value of 0. Because this value is less than 2, the following if construct evaluates to true and the continue statement executes:

    if (i < 5) continue;

    Because the continue statement ignores all the remaining statements in a for loop iteration, the control doesn’t print the value of the variable i, which leads the control to move on to the next for iteration. In the next for iteration, the value of the variable i is 5. The for loop condition evaluates to false and the control moves out of the for loop. After the for loop, the code prints out the value of the variable i, which increments once using the code i=i+5.

    The continue statement is used to skip the remaining steps in the current iteration and start with the next loop iteration. Let’s replace the break statement in the previous example with continue and examine its output:

    As soon as a loop encounters continue, it exits the current iteration of the loop. In this example, it skips the printing step for the array value Shreya. Unlike the break statement, continue doesn’t exit the loop—it restarts with the next loop iteration, printing the remaining array values (that is, Selvan and Harry).

    When you use the continue statement with nested loops, it exits the current iteration of the inner loop.

    Figure 5.22 compares how the control transfers out of the loop and to the next iteration when break and continue statements are used.

    Figure 5.22. Comparing the flow of control when using break and continue statements in a loop
    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest