Using continue in a switch statement
It's fine, the continue
statement relates to the enclosing loop, and your code should be equivalent to (avoiding such jump statements):
while (something = get_something()) {
if (something == A || something == B)
do_something();
}
But if you expect break
to exit the loop, as your comment suggest (it always tries again with another something, until it evaluates to false), you'll need a different structure.
For example:
do {
something = get_something();
} while (!(something == A || something == B));
do_something();
Should I ever use continue inside a switch statement?
PHP 7.3 or newer:
Using continue
to break a switch
statement is deprecated and will trigger a warning.
To exit a switch
statement, use break
.
To continue to the next iteration of a loop that's surrounding the current switch
statement, use continue 2
.
PHP 7.2 or older:
continue
and break
may be used interchangeably in PHP's switch
statements.
Why can't I use 'continue' inside a switch statement in Java?
Falling through is the standard behavior for a switch statement and so, consequently, using continue in a switch statement does not make sense. The continue statement is only used in for/while/do..while loops.
Based on my understanding of your intentions, you probably want to write:
System.out.println("default");
if ( (a == 'a') || (a == 'b') ){
System.out.println(a);
}
I would also suggest that you place the default condition at the very end.
EDIT:
It is not entirely true that continue statements cannot be used inside switch statements. A (ideally labeled) continue statement is entirely valid. For example:
public class Main {
public static void main(String[] args) {
loop:
for (int i=0; i<10; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 9:
continue loop;
}
System.out.println(i);
}
}
}
This will produce the following output:
0
2
4
6
8
Having a continue in a default case in a switch statement
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.
Any situation where I can't help using 'continue' within a switch statement?
It's used to to decide whether some statements after the switch statement should be executed. Here is an example:
#include <stdio.h>
#include <ctype.h>
int get_option(void)
{
int num;
do{
num = getchar();
} while(isspace(num));
if(isdigit(num))
return num - '0';
else
return -1;
}
int main(void)
{
int option;
while(1){
option = get_option();
switch(option){
case 0:
puts("You've entered 0");
continue; // <--- continue; gives you nothing
case 1:
puts("You've entered 1");
break; // <--- break; gives you "You've entered an odd number"
case 2:
puts("You've entered 2");
continue;
default:
puts("Input out of range!");
continue;
}
puts("You've entered an odd number"); // <--- Notice this line
}
}
Input and output:
0
You've entered 0
1
You've entered 1
You've entered an odd number
2
You've entered 2
3
Input out of range!
continue ALLWAYS Illegal in switch in JS but break works fine
continue
has absolutely nothing to do with switch
es, not in Javascript and not in C++:
int main()
{
int x = 5, y = 0;
switch (x) {
case 1:
continue;
case 2:
break;
default:
y = 4;
}
}
error: continue statement not within a loop
If you wish to break out of the case, use break
; otherwise, allow the case to fall through:
switch ("B")
{
case "A":
break;
case "B":
case "C":
break;
default:
break;
}
If you're looking for a shortcut to jump to the next case then, no, you can't do this.
switch ("B")
{
case "A":
break;
case "B":
if (something) {
continue; // nope, sorry, can't do this; use an else
}
// lots of code
case "C":
break;
default:
break;
}
I haven't found the answer to this, why continue in switch case affects the loop outside the switch but break doesn't break the loop?
B (and thereafter its descendants including C++, Java or JavaScript) overloads the break
keyword to jump out of switch
es.
So when you're in a nested loop/switch
situation, the break
keyword will apply to the innermost switch
or loop, whichever is closer.
This overload is an accident of history. B started by copying BCPL and while BCPL later gained an endcase
keyword design specifically for breaking switch
es, Thompson and Ritchie were not aware of the change and so B and C remained with their home-made readaptation of break
for this purpose.
(
http://port70.net/~nsz/c/c89/dmr_the_development_of_the_c_language.pdf
Not every difference between the BCPL language documented in
Richards’s book [Richards79] and B was deliberate; we started from an
earlier version of BCPL [Richards 67]. For example, the endcase that
escapes from a BCPL switchon statement was not present in the language
when we learned it in the 1960s, and so the overloading of the break
keyword to escape from the B and C switch statement owes to divergent
evolution rather than conscious change.
)
In any case, in B/C/C++, break
/continue
is nothing but syntactic sugar for a goto
and if you want to break
/continue
something other than the innermost loop/switch
, you can always achieve it by using an explicit goto
:
#include <stdio.h>
int main ()
{
int menu=0;
while (menu!=3)
{
scanf ("%d", &menu);
switch (menu)
{
case 1:
{
printf ("Case 1\n");
continue;
}
case 2:
{
printf ("Case 2\n");
goto break_loop;
//`return 0;` would work too in this case
break; //would break out of the switch
}
}
printf ("This doesn't get printed by case 1\n");
}
break_loop:
return 0;
}
Using `continue` keywoard in a switch nest inside a foreach loop
Yes, it continues the foreach
loop.
It is always useful to consult the documentation ;-)
The continue statement passes control to the next iteration of the
enclosing while, do, for, or foreach statement in which it appears.
or—more comprehensive—the C# language specification:
8.9.2 The continue statement
The continue statement starts a new iteration of the nearest enclosing
while, do, for, or foreach statement.The target of a continue statement is the end point of the embedded
statement of the nearest enclosing while, do, for, or foreach
statement. If a continue statement is not enclosed by a while, do,
for, or foreach statement, a compile-time error occurs.When multiple while, do, for, or foreach statements are nested within
each other, a continue statement applies only to the innermost
statement. To transfer control across multiple nesting levels, a goto
statement (§8.9.3) must be used.A continue statement cannot exit a finally block (§8.10). When a
continue statement occurs within a finally block, the target of the
continue statement must be within the same finally block; otherwise a
compile-time error occurs.A continue statement is executed as follows:
If the continue statement exits one or more try blocks with
associated finally blocks, control is initially transferred to the
finally block of the innermost try statement. When and if control
reaches the end point of a finally block, control is transferred to
the finally block of the next enclosing try statement. This process is
repeated until the finally blocks of all intervening try statements
have been executed.Control is transferred to the target of the continue
statement.
Because a continue statement unconditionally transfers control
elsewhere, the end point of a continue statement is never reachable.
Using continue in a switch statement
It's fine, the continue
statement relates to the enclosing loop, and your code should be equivalent to (avoiding such jump statements):
while (something = get_something()) {
if (something == A || something == B)
do_something();
}
But if you expect break
to exit the loop, as your comment suggest (it always tries again with another something, until it evaluates to false), you'll need a different structure.
For example:
do {
something = get_something();
} while (!(something == A || something == B));
do_something();
Related Topics
How to Perform a Pairwise Binary Operation Between the Elements of Two Containers
Template Class Member Function Only Specialization
Noexcept, Stack Unwinding and Performance
Function Template with an Operator
Load Shared Library by Path at Runtime
Cannot Use .Begin() or .End() on an Array
Clear Data Inside Text File in C++
C++ Bool Returns 0 1 Instead of True False
Can You Use Keyword Explicit to Prevent Automatic Conversion of Method Parameters
In C/C++ Why Does the Do While(Expression); Need a Semi Colon
How to Iterate Through a List of Objects in C++
Call of Overloaded Function Is Ambiguous
Are All Temporaries Rvalues in C++
C++ on Small-Footprint Microcontrollers
How to Link Libcurl to My C++ Program in Linux