10

Why is it that the following code:

class swi  
{
    public static void main(String[] args)  
    {  
        int a=98;
        switch(a)
        {
            default:{ System.out.println("default");continue;}
            case 'b':{ System.out.println(a); continue;}
            case 'a':{ System.out.println(a);}
        }
        System.out.println("Switch Completed");
    }
}

Gives the error:

continue outside of loop

James P.
  • 19,313
  • 27
  • 97
  • 155
abson
  • 9,148
  • 17
  • 50
  • 69
  • 1
    Syntactically i too agree but what's the logic behind this why can't one use continue where one can use break? – abson Mar 26 '10 at 07:48
  • @abson: Because `continue` and `break` have different purposes, and while `break` is relevant to both `switch` and to loops, `continue` is not -- there's no "top" to jump back to. – T.J. Crowder Mar 26 '10 at 07:51
  • Sorry i got it switch is just conditional statement and not a looping construct so it is, is it? – abson Mar 26 '10 at 07:52
  • @abson: That's right, `switch` is not a loop, it's an indexed lookup (basically an optimized if/else/else/else with no duplicate conditions allowed). – T.J. Crowder Mar 26 '10 at 07:53
  • @T.J.Crowder sorry for that late comment but i want to point out 1 thing: you said it is a optimized `if/else/else/else`, but thats only if you are using `break;` at the end of each `case`, what is the normal use of a `switch`. If you don't use break it is like: `if something`/`if something || anotherThing`/`if something || anotherThing || somethingElse`/... As much as i understood the OP wanted it to use as an `if/if/if/if`, but thats not possible with a switch right? – Robert P Feb 25 '14 at 13:39
  • 2
    @Springrbua: Right, only if you use `break`. If you don't, it's basically a goto, because you jump to the matching case, but then when you reach the end of that case you continue with the logic for the next, even though that case doesn't match, and so on until you fall out the bottom of the `switch`. – T.J. Crowder Feb 25 '14 at 13:45

7 Answers7

25

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

Flow
  • 23,572
  • 15
  • 99
  • 156
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
8

The continue-Statement may be used in loops and not in switch. What you probably want is a break.

stefanglase
  • 10,296
  • 4
  • 32
  • 42
6

Because you have a continue outside of a loop. continue is for jumping to the next iteration of a loop, skipping the remainder of the current iteration. But you don't have any loop in that code. What you want for breaking out of a switch case block is the keyword break (see below).

There's also no need to put every case block within braces (unless you want locally-scoped variables within them).

So something a bit like this would be more standard:

class swi22
{
    public static void main(String[] args)
    {
        int a=98;
        switch(a)
        {
            default:
                System.out.println("default");
                break;
            case 'b':
                System.out.println(a);
                break;
            case 'a':
                System.out.println(a);
                break;
        }
        System.out.println("Switch Completed");
    }
}

There's also a school of thought that says the default condition should always be at the end. This is not a requirement, just a fairly widely-used convention.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Shouldn't you use break instead of continue?

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
1

continue simply moves directly to the next iteration of the loop.

break is used to break out of loops and switches.

Use break; instead of continue;

Continue:

for(x = 0; x < 10; x++)
{
   if(x == 3)
     continue;
   else       
     DoIterativeWork();       
}

Switch:

switch(a)
{
 default:{ System.out.println("default"); break;}
 case 'b':{ System.out.println(a); break;}
 case 'a':{ System.out.println(a);}
}
Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
0

You are using continue where you should be using break

Scott Smith
  • 3,900
  • 2
  • 31
  • 63
-1

continue inside switch??!!! only break can be kept inside switch.!

ur code should be,

class swi22  
{
     public static void main(String[] args)  
     {  
         int a=98;
         switch(a)
         {
             default:{ System.out.println("default");break;}
             case 'b':{ System.out.println(a); break;}
             case 'a':{ System.out.println(a);}
          }
          System.out.println("Switch Completed");
     }
}
raj
  • 3,769
  • 4
  • 25
  • 43