Having continued in default case in switch statement

If I had this:

do { int x = scannerScan.nextInt(); switch(x) { case 1: System.out.println("Stuff"); break; case 2: System.out.println("Pink cows are fluffy and can fly."); default: continue; } } while(true); 

What happens if the default case is reached? I tried to find stuff on the Internet and Stackoverflow, but couldn't find anything to continue in the default case of the Java language.

+8
java switch-statement
source share
3 answers

continue in the loop

The continue statement skips the current iteration of for , while , or do-while . The unnoticed form is skipped to the end of the innermost body of the loop and evaluates the logical expression that the loop controls. [...]

In your code, a while(true); will be continued. This statement does not affect the switch code block.

+10
source share
Operators

continue in switch are not special. It would move to the loop state (end of the loop body), as if it were inside the loop, but outside the switch .

It does nothing in this particular piece of code.

+8
source share

Compare break statement :

Operator

A break tries to transfer control to the innermost shell switch , while , do or for statement & hellip;

With continue statement :

Operator

A continue tries to transfer control to the innermost conclusion while , do or for & hellip;

Thus, continue refers to the do...while and:

& hellip; then immediately completes the current iteration and starts a new one.

+6
source share

All Articles