• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exam Watch p. 316 re curly braces (K&B7)

 
Ranch Hand
Posts: 67
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello:

According to the exam watch, the first code snippet violates a rule for the switch and case arguments because it "uses a curly brace ...". However when I compile and run it all works fine, and according to StackOverflow, while not using curly braces in a case might make the code more readable with respect to scope, and such, it is not really an illegal violation. Also, shouldn't "uses a curly brace" be plural, ie., "uses curly braces" ? Since there are two of them... Or, perhaps they intended for there to be one curly brace missing in the case in the first example?



Also, the second snippet uses curly braces and it compiles and runs fine.

Thank you,

Mark
 
Sheriff
Posts: 11606
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

Mark Kevin wrote:According to the exam watch, the first code snippet violates a rule for the switch and case arguments because it "uses a curly brace ...". However when I compile and run it all works fine, and according to StackOverflow, while not using curly braces in a case might make the code more readable with respect to scope, and such, it is not really an illegal violation. Also, shouldn't "uses a curly brace" be plural, ie., "uses curly braces" ? Since there are two of them... Or, perhaps they intended for there to be one curly brace missing in the case in the first example?


You should look more carefully at the code snippet. There's nothing wrong with the curly braces. Something else is missing and that's why the code snippet fails to compile.

Mark Kevin wrote:Also, the second snippet uses curly braces and it compiles and runs fine.


The 2nd code snippet fails to compile as well. It uses curly braces as well, but again something else is missing. Again look more closely!

Having an eye for detail is very important when you take the actual exam! And this Exam Watch shows why

Hope it helps!
Kind regards,
Roel

 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know that in the first example the colon is missing (which is why I inserted it), and in the second the keyword case is missing (also inserted by me).
I put those in in order to make it compile. But that's not the issue I'm addressing. I'm speaking of the curly braces.
Here's the Exam Watch quote: " In the first example , the case uses a curly brace and omits the colon." A normal reading of this statement indicates to me that using a curly brace is an error. But it's not.

m.
 
Roel De Nijs
Sheriff
Posts: 11606
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

Mark Kevin wrote:Here's the Exam Watch quote: " In the first example , the case uses a curly brace and omits the colon." A normal reading of this statement indicates to me that using a curly brace is an error. But it's not.


Using an opening and a closing curly brace with a case is indeed not a compiler error, omiting the colon definitely is. So to avoid confusion, I'll add this one to the errata overview. On the following pages you'll see plenty of examples where a case uses curly braces.
 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you, Roel. On behalf of a lot of people, your unselfish dedication and help is greatly appreciated.

Mark
 
Roel De Nijs
Sheriff
Posts: 11606
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
Added to the errata overview.
reply
    Bookmark Topic Watch Topic
  • New Topic