Results 1 to 11 of 11
  1. #1
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Question Not sure what my code does

    Hello Fellow Javers,

    Right now I am currently learning the 'continue' statement in a For Loop. I have made this code with the continue, but I don't really know how this code works. I just stumpled upon it when I was coding. Please tell me what and how this code works if you can.

    Thanks

    - SimeonTG

    Java Code:
    public class ContinueLabels {
    	public static void main (String args[]) {
    		
    		Loop1: 
    			for (int var = 1; var < 100; var++){
    				System.out.println("\nOuter loop pass " + var + 
    						  ", Inner loop: ");
    				
    				// Inner loop
    				for (int j = 1; j < 100; j++) {
    					if (j == 50) continue Loop1; 
    					System.out.print(j);
    				}
    			}
    		
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Not sure what my code does

    What is printed when the code executes? Please copy and paste it here.
    To make for less output, change the 100s to 10, the 50 to 5 (drop off the ending 0)

    The output will help us explain what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Not sure what my code does

    This is what the code does:
    (Sorry for the late reply)


    Outer loop pass 1, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 2, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 3, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 4, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 5, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 6, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 7, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 8, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 9, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 10, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 11, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 12, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 13, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 14, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 15, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 16, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 17, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 18, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 19, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 20, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 21, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 22, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 23, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 24, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 25, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 26, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 27, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 28, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 29, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 30, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 31, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 32, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 33, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 34, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 35, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 36, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 37, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 38, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 39, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 40, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 41, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 42, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 43, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 44, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 45, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 46, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 47, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Not sure what my code does

    I guess you missed this:
    To make for less output, change the 100s to 10, the 50 to 5 (drop off the ending 0)

    Outer loop pass 1, Inner loop:
    12345678910111213141516171819202122232425262728293 031323334353637383940414243444546474849
    Outer loop pass 2, Inner loop:
    Look at the numbers on the second line. There aren't any spaces between them, but you can see that the last number printed is 49.
    Now look at this statement in the code:
    Java Code:
      if (j == 50) continue Loop1;   // go back to Loop1 at 50
    That exits the inner loop before the 50 is printed.

    The 3rd line above shows that execution continued at the top of the outer loop where the print statement is that printed that line.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Not sure what my code does

    the inside loop is supposed to count from 1 to 100, but when it reaches 50 it jumps back to the outside loop, makes that tick one up and then starts from scratch. The author is trying to show the difference to "break", I would guess.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Not sure what my code does

    show the difference to "break",
    Try it and see what happens. Compare the output to that of the code with the continue statement.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Not sure what my code does

    Oh ok. Makes a lot of sense now. Thanks! But who would use something like this in real world applications?

  8. #8
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Not sure what my code does

    Quote Originally Posted by benji2505 View Post
    the inside loop is supposed to count from 1 to 100, but when it reaches 50 it jumps back to the outside loop, makes that tick one up and then starts from scratch. The author is trying to show the difference to "break", I would guess.
    This is what I get when it breaks:


    Outer loop pass 10, Inner loop:
    1
    Outer loop pass 11, Inner loop:
    1
    Outer loop pass 12, Inner loop:
    1
    Outer loop pass 13, Inner loop:
    1
    Outer loop pass 14, Inner loop:
    1
    Outer loop pass 15, Inner loop:
    1
    Outer loop pass 16, Inner loop:
    1
    Outer loop pass 17, Inner loop:
    1
    Outer loop pass 18, Inner loop:
    1
    Outer loop pass 19, Inner loop:
    1
    Outer loop pass 20, Inner loop:
    1
    Outer loop pass 21, Inner loop:
    1
    Outer loop pass 22, Inner loop:
    1
    Outer loop pass 23, Inner loop:
    1
    Outer loop pass 24, Inner loop:
    1
    Outer loop pass 25, Inner loop:
    1
    Outer loop pass 26, Inner loop:
    1
    Outer loop pass 27, Inner loop:
    1
    Outer loop pass 28, Inner loop:
    1
    Outer loop pass 29, Inner loop:
    1
    Outer loop pass 30, Inner loop:
    1
    Outer loop pass 31, Inner loop:
    1
    Outer loop pass 32, Inner loop:
    1
    Outer loop pass 33, Inner loop:
    1
    Outer loop pass 34, Inner loop:
    1
    Outer loop pass 35, Inner loop:
    1
    Outer loop pass 36, Inner loop:
    1
    Outer loop pass 37, Inner loop:
    1
    Outer loop pass 38, Inner loop:
    1
    Outer loop pass 39, Inner loop:
    1
    Outer loop pass 40, Inner loop:
    1
    Outer loop pass 41, Inner loop:
    1
    Outer loop pass 42, Inner loop:
    1
    Outer loop pass 43, Inner loop:
    1
    Outer loop pass 44, Inner loop:
    1
    Outer loop pass 45, Inner loop:
    1
    Outer loop pass 46, Inner loop:
    1
    Outer loop pass 47, Inner loop:
    1
    Outer loop pass 48, Inner loop:
    1
    Outer loop pass 49, Inner loop:
    1
    Outer loop pass 50, Inner loop:
    1
    Outer loop pass 51, Inner loop:
    1
    Outer loop pass 52, Inner loop:
    1
    Outer loop pass 53, Inner loop:
    1
    Outer loop pass 54, Inner loop:
    1
    Outer loop pass 55, Inner loop:
    1
    Outer loop pass 56, Inner loop:
    1
    Outer loop pass 57, Inner loop:
    1
    Outer loop pass 58, Inner loop:
    1
    Outer loop pass 59, Inner loop:
    1
    Outer loop pass 60, Inner loop:
    1
    Outer loop pass 61, Inner loop:
    1
    Outer loop pass 62, Inner loop:
    1
    Outer loop pass 63, Inner loop:
    1
    Outer loop pass 64, Inner loop:
    1
    Outer loop pass 65, Inner loop:
    1
    Outer loop pass 66, Inner loop:
    1
    Outer loop pass 67, Inner loop:
    1
    Outer loop pass 68, Inner loop:
    1
    Outer loop pass 69, Inner loop:
    1
    Outer loop pass 70, Inner loop:
    1
    Outer loop pass 71, Inner loop:
    1
    Outer loop pass 72, Inner loop:
    1
    Outer loop pass 73, Inner loop:
    1
    Outer loop pass 74, Inner loop:
    1
    Outer loop pass 75, Inner loop:
    1
    Outer loop pass 76, Inner loop:
    1
    Outer loop pass 77, Inner loop:
    1
    Outer loop pass 78, Inner loop:
    1
    Outer loop pass 79, Inner loop:
    1
    Outer loop pass 80, Inner loop:
    1
    Outer loop pass 81, Inner loop:
    1
    Outer loop pass 82, Inner loop:
    1
    Outer loop pass 83, Inner loop:
    1
    Outer loop pass 84, Inner loop:
    1
    Outer loop pass 85, Inner loop:
    1
    Outer loop pass 86, Inner loop:
    1
    Outer loop pass 87, Inner loop:
    1
    Outer loop pass 88, Inner loop:
    1
    Outer loop pass 89, Inner loop:
    1
    Outer loop pass 90, Inner loop:
    1
    Outer loop pass 91, Inner loop:
    1
    Outer loop pass 92, Inner loop:
    1
    Outer loop pass 93, Inner loop:
    1
    Outer loop pass 94, Inner loop:
    1
    Outer loop pass 95, Inner loop:
    1
    Outer loop pass 96, Inner loop:
    1
    Outer loop pass 97, Inner loop:
    1
    Outer loop pass 98, Inner loop:
    1
    Outer loop pass 99, Inner loop:
    1

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Not sure what my code does

    This is what I get when it breaks:
    Please post the code that creates that output. It looks like you changed more than the continue.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Not sure what my code does

    Ok.


    public class ContinueLabels {
    public static void main (String args[]) {

    Loop1:
    for (int var = 10; var < 100; var++){
    System.out.println("\nOuter loop pass " + var +
    ", Inner loop: ");

    // Inner loop
    for (int j = 1; j < 100; j++) {
    if (j == 5) continue Loop1;
    System.out.print(j);
    break;
    }
    }

    }
    }

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Not sure what my code does

    Quote Originally Posted by SimeonTG View Post
    But who would use something like this in real world applications?
    Actually, I find it quite useful and have used it a number of times. Sometimes, the logic is simply
    clearer for me. Other times, it makes the code a bit cleaner. For example:

    Java Code:
    while (str = readLine()) {
      if (str.contains("foo") || str.contains("bar"))
          continue;
      // Now do lots of processing on strings that don't contain the words
      // foo or bar
    }
    You could also do the same thing as follows::

    Java Code:
    while (str = readLine()) {
        if(!(str.contains("foo") || str.contains("bar"))) {
           // now do lots of processing on strings that don't contain the words
          // foo or bar
        }
    }

    It's one of those constructs which seems to fit certain situations. In the above
    situation I would rather use the first method because:

    1) It gets rid of a conditional block of code so for me, it's cleaner.
    2) I can easily add another test to skip over the processing instead
    of using a more complicated logical if statement.

    Java Code:
    while (str = readLine()) {
      if (str.contains("foo") || str.contains("bar"))
          continue;
      if (str.contains("apple")
          continue;
      // Now do lots of processing on strings that don't contain the words
      // foo or bar or apple
    }
    There may be some hidden overhead in the use of the continue statement. Or the compiler could simply optimize
    it to a more efficient form. I really don't know.

    Regards,
    Jim
    Last edited by jim829; 11-04-2015 at 05:03 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Posting code to forum : Code to code tags
    By rushtojess in forum Suggestions & Feedback
    Replies: 2
    Last Post: 04-21-2015, 08:16 PM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •