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

continue in switch block

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • 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?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "break" can be used in switch a statement, but "continue" is for loops.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic