Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a continue in a default case in a 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 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.

like image 921
Ungeheuer Avatar asked Apr 15 '15 23:04

Ungeheuer


People also ask

Can you use continue in a switch statement?

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.

Is it possible to put a default between cases in switch?

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.

Why continue statement Cannot be used in switch?

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.

Can continue be used in switch case in C?

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.


2 Answers

continue statement in the loop

The continue statement skips the current iteration of a for, while , or do-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.

like image 182
MaxZoom Avatar answered Oct 01 '22 08:10

MaxZoom


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.

like image 35
user253751 Avatar answered Oct 01 '22 09:10

user253751