Results 1 to 3 of 3
  1. #1
    er1c550n20 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default use Continue with label

    The prog given in below prog is very very simple but i am having hard time understanding the sequence of flow of control plus increment of i & j at different stage. Can some1 take time explain it with differnt incrementing stage of i & j.

    // Using continue with a label.
    Java Code:
    class ContinueLabel
       {
         public static void main(String args[]) 
         	{
               outer: for (int i=0; i<10; i++) 
               	 {
                   for(int j=0; j<10; j++) 
                   	{
                       if(j > i)
                       	 {
                           System.out.println();
                           continue outer;
                         }
                        System.out.print(" " + (i * j));
                    }
                }
                System.out.println();
            }
    }
    the output is:
    0
    0 1
    0 2 4
    0 3 6 9
    0 4 8 12 16
    0 5 10 15 20 25
    0 6 12 18 24 30 36
    0 7 14 21 28 35 42 49
    0 8 16 24 32 40 48 56 64
    0 9 18 27 36 45 54 63 72 81
    Last edited by Eranga; 04-04-2010 at 02:15 PM. Reason: added code tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default

    You can read 'continue outer' as 'stop doing everything you're doing and continue the next loop iteration pointed to by the "outer" label'. So here the inner loop stops every time j > i and the whole thing continues from the top with the next value for variable i.

    kind regards,

    Jos

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In other words, the continue statement skips the current iteration of loop, such as for, while , or do-while loop.

    In outer for loop, variable i initialize to zero. And then enter to inner for loop, initialize variable j to zero there as well.

    Then it checks the conditions j > i. Since the condition fail, following print statement execute, and print the value zero. After that j increment by one. Now it's two. Check the condition and success. Means that body of the if is execute. Print a new line to the console. Then mover to continue outer; which instruct to VM to stop the current iteration and move to the place which you define as a label, outer

    From there start the execution again, initialize values from zero and so on as above.

    Hope it's helpful to you.

Similar Threads

  1. Continue or not?
    By kliane in forum New To Java
    Replies: 3
    Last Post: 01-19-2010, 03:42 PM
  2. Program doesn't continue...
    By Reiyn in forum New To Java
    Replies: 7
    Last Post: 10-07-2008, 11:28 PM
  3. How to use Continue with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:46 PM
  4. How to use Continue
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •