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 would happen if the default case were to be reached? I tried to find stuff on the internet and Stackoverflow, but could not find anything about a continue in the default case that had to do with the Java language.
We can not use a continue with the switch statement. The break statement terminates the whole loop early. The continue statement brings the next iteration early. It stops the execution of the loop.
The default statement doesn't have to come at the end. It may appear anywhere in the body of the switch statement. A case or default label can only appear inside a switch statement. The type of switch expression and case constant-expression must be integral.
The continue statement isn't used with the switch statement, but it can be used in a while, do-while, or for loop. The break statement ends the loop prematurely. The continue statement signals the start of the following iteration. It halts the loop's execution.
The continue statement is not used with the switch statement, but it can be used within the while loop, do-while loop, or for-loop.
continue statement in the loop
The continue statement skips the current iteration of a
for
,while
, ordo-while
loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. [...]
In your code the loop while(true);
will continue.
The statement makes no effect on the switch
code block.
continue
statements in switch
statements are not special. It would jump to the loop condition (the end of the loop body), just as it would if it was inside the loop but outside the switch
.
In this particular code snippet, it effectively does nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With