• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Question about continue in while loop  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from Enthuware:

Using a continue in a while loop causes the loop to break the current iteration and start the next iteration of the loop.
You have to choose 1 option:
  • Ture
  • False

  • The answer given was True.

    I have doubt about the correctness about this answer, because if you use continue with label, yes, the continue statement will break the current iteration, but it might not cause the start of next iteration of the loop. So IMO the answer should be False. I have the following code to demonstrate my point:



    The print out is, as you can see the output, the continue statement caused the labeled block to execute again.
     
    Ranch Hand
    Posts: 276
    Chrome Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    May be you put a print statement right after your outer-for-loop starts and see for yourself if the loop-local variable j is incremented or not.. if you see j incrementing, then you can conclude that 'continue' actually starts the next iteration rather than executing the whole loop from the start.
     
    Jim Jonides
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good suggestion. After I inserted the print statement at the end of the outer for loop like following, it did not get executed, the printout stays the same. This means the continue + label cause the outer for loop to start all over again.

     
    Ranch Hand
    Posts: 3091
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's not the end of the outer loop - it's the end of the middle loop.
     
    author
    Sheriff
    Posts: 23588
    138
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Keep in mind that the test is for programmers -- not lawyers. There will be questions that will have some edge conditions. In this case, you are assuming a label loop (although none is mentioned), and then assuming the loop being referred to isn't the label loop that you assumed earlier. If the question mentioned labels, then okay, but otherwise, I think it is safer to assuming the more common case.

    You only have so much time for the test. Play a bit of the percentages here is probably good. And over thinking the question is probably bad.

    Henry
     
    Ranch Hand
    Posts: 448
    Eclipse IDE Firefox Browser Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, pay attention to the words of question...
    They may be worded to trick you.
     
    Jim Jonides
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for all the inputs!
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!