• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for (;;) continue;

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a little confusion over this mock exam question for Java 8 cert, if anyone can help a little ?

The question asks what will be printed with the following line of code



What threw me here was the 'continue' and 'break' statements on the same line as the for loop, which, in my mind is valid / same as putting it on the next line.........or maybe not
So, i reformatted the code into how i would normally expect to see for loops (curly braces etc) in order to better understand the 'continue' and 'break' (for the record - i understand what both continue and break do - i've just not seen them on the same line as the for loops before)

Here is my code reformatting of the question...



However, this doesn't compile due to the continue / break now being 'inside' the loop body - can anyone help me understand the continue / break being part of the loop condition(?) or add some comment on the way its used in the question ?

Thanks
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those loops are not nested.
This is the equivalent code

By the way, please quote your sources (QuoteYourSources ← click)
 
Steven Barn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pawel - that makes perfect sense now - couldn't see the wood for the trees !

I'll quote the source next time too - for the record this was from the Enthuware exams, which seem pretty good IMHO

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And have you managed to translate Paweł's line 8 into English proper Java?

I suspect the same question and the same answer would have been valid in every version of Java since 1.0.0.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what did you say would be printed from that code? Is there a print statement you haven't shown us?
[edit]I see there are 3 print statements in OP's second code block which don't seem to appear anywhere else.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing that in the end the value of i was printed.
 
Steven Barn
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit (apologies) the question from the exam was actually - "What will be the result of attempting to compile and run the following program?"
and the answer, again from the exam was - "The code will compile without error and will terminate without problems when run."

I was playing with these for loops a little more too and thought it worth pointing out some of the 'sneakier' questions

ie


versus this



This sort of baffled me at first, while the for loops in the first statement, resulted in the system out statement being reached, whereas the second code block did'nt - i found it in the end - but can you spot it too ?

Thanks


 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, of course we can see the difference. You should always tell the full details; you caused confusion by asking what is printed when there were no print statements.

And if you did manage to enter the last loop, what would be the result of running that code? With or without print statements?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit more specific, what is the body of the third loop in both cases?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic