Thread: The "continue" statement in JAVA

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    The "continue" statement in JAVA

    right now I have a code of

    Code:
    while (!eof) {
    for (int i=0; condtion; i++) {
    if (conditionX==-1) break;
    blah blah;
    }
    
    if (conditionX==-1) continue;
    more blah blah}
    what I want to know is about the use of the "continue", does it just fallout for while loops? or can it fall out for "for loops" too.
    I mean can I change
    Code:
    if (conditionX==-1) break;
    to

    Code:
    if (conditionX==-1) continue
    and still have it working like the original code (falling out the *complete* while and not just the for loop.)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    In Java, continue will end execution of the nearest loop's current iteration and move to the next. So:
    Code:
    for ( int i = 0; i < 10; i++ ) {
      if ( i == 5 )
        continue;
      System.out.println ( "Iteration: " + i );
    }
    
    Output:
    
    Iteration 0
    Iteration 1
    Iteration 2
    Iteration 3
    Iteration 4
    Iteration 6
    Iteration 7
    Iteration 8
    Iteration 9
    In a nested loop, the inner-most loop is the one that break or continue has an effect on:
    Code:
    for ( int i = 0; i < 2; i++ ) {
      for ( int j = 0; j < 2; j++ ) {
        if ( i == 0 )
          continue;
        System.out.println ( "Iterations: " + i + " " + j );
      }
    }
    
    Output:
    
    Iterations: 1 0
    Iterations: 1 1
    Break, on the other hand, terminates execution of the nearest loop or switch statement. Execution does not move to the next iteration, it goes to the next statement after the loop:
    Code:
    for ( int i = 0; i < 10; i++ ) {
      if ( i == 5 )
        break;
      System.out.println ( "Iteration: " + i );
    }
    
    Output:
    
    Iteration 0
    Iteration 1
    Iteration 2
    Iteration 3
    Iteration 4
    The same goes for nested loops as with continue. The inner-most loop is the one acted on:
    Code:
    for ( int i = 0; i < 2; i++ ) {
      for ( int j = 0; j < 2; j++ ) {
        if ( i == 0 )
          break;
        System.out.println ( "Iterations: " + i + " " + j );
      }
    }
    
    Output:
    
    Iterations: 1 0
    Iterations: 1 1
    If you want to break from a nested loop, you can use a labeled break:
    Code:
    outer:
    for ( int i = 0; i < 2; i++ ) {
      for ( int j = 0; j < 2; j++ ) {
        if ( i == 1 )
          break outer;
        System.out.println ( "Iterations: " + i + " " + j );
      }
    }
    
    Output:
    
    Iterations: 0 0
    Iterations: 0 1
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's up with Java programmers, anyway?
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 76
    Last Post: 06-11-2009, 06:17 AM
  2. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 09:09 AM
  3. C++ and Java :: Breakdown
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-25-2004, 06:56 AM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM