Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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!
 
Paweł Baczyński
Bartender
Posts: 1878
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1878
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Esbjörn Rundberg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic