• 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

break and continue statement in if, table 2.5, page 91 (Java OCA 8 Programmer I Study Guide)

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am writing regarding pag 91. The table 2.5 states that IF loop doesn't allow both break and continue statement even if in the pages before there many example where break and continue are used inside an IF loop. Can you please help me ?

Thank you!
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The if statement is not a loop.

Actually, you can break out of an if.This prints:
Done

This doesn't compile (and if it did compile it would be broken anyway...)
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so it means that break and continue in the IF are only allowed if a label is provided ? Thank you!
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, continue is not allowed, even with a label.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I should correct the first line of this table : Break statement YES allowed (when LABEL is provided ) ... Am I right ? Thank you!
unnamed.png
[Thumbnail for unnamed.png]
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Road wrote:So I should correct the first line of this table : Break statement YES allowed (when LABEL is provided ) ... Am I right ?


The first line of table 2.5 should beIf you check this page of Oracle's Java tutorial about branching statements, it's not mentioned that a labeled break statement can be used together with an if statement. It's only discussing the different loops and the switch statement.

Hope it helps!
Kind regards,
Roel
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt,
Thanks for noticing this! I've added it to our errata list and credited you. I have a different fix than Roel's though. I'd go with renaming the table header to be allows unlabeled break statements.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:No, continue is not allowed, even with a label.



Hi Pawel!

Question 18 in the same chapter uses a continue with a label in an if statement and it appear to work. Am I missing something?

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Esbjörn Rundberg,

First of all, a warm welcome to CodeRanch!

Esbjörn Rundberg wrote:Question 18 in the same chapter uses a continue with a label in an if statement and it appear to work. Am I missing something?


Yes, you are definitely missing something Yhe continue statement is inside a loop. Just remove both for loops and see what happens...

Hope it helps!
Kind regards,
Roel
 
Esbjörn Rundberg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Now I see, I wasn't paying enough attention when reading the thread.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Esbjörn Rundberg wrote:Now I see, I wasn't paying enough attention when reading the thread.


So you already discovered an area which you must improve for the actual exam If you don't pay enough attention to the question, code snippet and possible answers; it's almost impossible to answer the question correctly (unless you are very lucky).
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic