• 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

continue in switch block

 
Ranch Hand
Posts: 182
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from Robert Liguori , OCA programmer guide.
Ques:continue and break statements are allowed within what type of statements?
A.Loop Statements
B.All conditional statements
C.The Switch Statement.
D.Expression Statements.

The answer is A and C.
Explanation: Iteration and switch statements can include continue and break statements. Iteration statements known as loop include the for loop, enhanced for loop, while loop and do-while loop. B and D are incorrect. The only conditional statement that can include the break and continue statement is switch statement. There is no logical reason to continue or break out of an expression statement.

This explanation doesnt seem right. I think only 'A' can be right. We can have a break but we cannot have continue in switch, it would throw a compiler error.I understand we could have switch inside for or do-while and then have a continue.



But does this make the statement 'C' true?
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "break" can be used in switch a statement, but "continue" is for loops.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramya Subraamanian wrote:But does this make the statement 'C' true?


That's easy to verify with a quick and simple code snippetIf you try to compile this code snippet, you'll get a compiler error on line1: "continue outside of loop" (or something similar). So a continue statement can not be used inside a switch statement.

And you could indeed use a continue statement in a switch statement if this switch statement is used inside a loop.This code snippet compiles successfully, but this does not make answer C true! If that would have been the case then the correct answer would have been B because then you could use a continue statement in an if statement too But when using the continue statement in an if statement without a loop, you'll get the same compiler error as with the switch statementPlease note that it does not make any difference if you have a labeled or unlabeled continue statement,

Hope it helps!
Kind regards,
Roel

PS. It was reported a few years ago as an errata for the OCA Java SE 7 study guide, but it seems it was not processed by the authors.
 
Ramya R Subramanian
Ranch Hand
Posts: 182
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code snippet compiles successfully, but this does not make answer C true! If that would have been the case then the correct answer would have been B because then you could use a continue statement in an if statement too


Thats what I thought as well. But the explanation given for this answer, got me confused.

Thanks for clarifying, Scott and Roel.

And I found the question in the mock exam.I don't think it's there in the book(pdf).

switch_continue1.png
[Thumbnail for switch_continue1.png]
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As someone who wrote two books and exam questions, I'm rather familiar with unintentional errors. Most likely, the first version of that question just had the word 'break' in it and they updated to include 'continue' without updating the answer guide. Just a guess though!
 
reply
    Bookmark Topic Watch Topic
  • New Topic