• Post Reply Bookmark Topic Watch Topic
  • New Topic

Continue in Switch statement?  RSS feed

 
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am quite confused because I encountered the following contradiction:
In the book I read that I cannot put continue in a switch statement.. Switch takes only break statement.
Although when taking a test on Java,
I encountered the following question:


The continue and break statements are allowed within what types of statements?(pick all that apply)

A. Loop statements
B. Conditional statements
C. switch statement
D. Expression statements




I answered A because I thought that continue does not go in switch.
But I answered wrong. Here is the correct answer according to this test:

A and C are correct. Iteration statements and switch statements can include continue and break statements. Iteration statements , known as loops, include the for loop, enhanced for loop, while loop and do-while loop.


So the question is: Can you indeed put a continue statement in a switch statement??
 
Rancher
Posts: 1816
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer doesn't seem to be correct.  If the continue is inside any switch case construct, the construct itself should be inside any looping construct.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such quotes come from, to avoid copyright problems.

The correct way to sort out that question is to look in the Java® Language Specification (=JLS), but that can be difficult to read. And I don't think that link helps, so let's try a different JLS section. And I don't think that is much better. Try the JLS about continue.
That JLS Section wrote:A continue statement may occur only in a while, do, or for statement; statements of these three kinds are called iteration statements. Control passes to the loop-continuation point of an iteration statement.
Success

So whoever said you can write continue in a switch statement was mistaken. That makes it more important to find the sources so we can verify which is correct.
Another way to find out is to write such code and try to compile it.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks ! So i am not confused !! so this question was from the OCA Java SE 8 Programmer I Study Guide cd-rom that came along with the book from Oracle Press.
 
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastic Dey has hit on the subtlety that I think is the source of your confusion. If the continue is in a switch statement that is itself in an iteration statement like a while or for loop, the continue will be allowed, just as it would be if it was in a conditional statement that was itself inside a loop. If there was no enclosing loop statement, putting continue in a switch or conditional statement would cause a compile-time error.

The book is still wrong either way because if it counted the switch as a correct answer for the reason I just explained, then you also need to consider conditional statements as correct for the same reasons. The best answer is just A, in loop statements.
 
Swastik Dey
Rancher
Posts: 1816
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I couldn't explain it in proper way, but that's what I wanted to say.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . The book is still wrong either way . . .
Something I had to learn about tests is that they must not only have a right answer, but also every answer not right must be very definitely wrong. Junilu is correct that making C a right answer vitiates B as a wrong answer.
What is an expression statement? Is that a term used by the JLS? Yes, it is. I hadn't heard it before. One example is i++; which is both an expression and a statement in its own right. System.out.println("Hello world"); is another, and there is in the same section another answer to Bloch and Gafter's question about where you can break Java® code by adding ().

Joshua Bloch and Neal Gafter: Java Puzzlers. They say (I think: not got my copy to hand) there is only one case where it is possible to break Java® code by adding () but that JLS section adds another such situation.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this same question also here:
https://coderanch.com/t/662040/certification/continue-switch-block
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few hours ago, I wrote:. . . System.out.println("Hello world"); is another, and there is in the same section another answer to Bloch and Gafter's question about where you can break Java® code by adding ().

Joshua Bloch and Neal Gafter: Java Puzzlers. They say (I think: not got my copy to hand) there is only one case where it is possible to break Java® code by adding () but that JLS section adds another such situation.
See that book, pages 209-210.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!