user2881409
user2881409

Reputation: 7

Explanation on Switch Statements

public class g{
    public static void main(String [] args){
    for(int x = 1; x <17; x +=3){
        switch(x){
            case -1: case 0: case 1:
                System.out.print("Breeze");
            case 2: System.out.print("Easy");
            case 3:
            case 4: System.out.print("As"); break;
            case 5: System.out.print("Pie"); break;
            case 6: case 7: System.out.print("No");
            case 8: System.out.print("Problem");
            case 9: break;
            case 10: System.out.print("Like");
            case 12: System.out.print("Nothing"); break;
            case 13:
            case 14: System.out.print("phew"); break;
        }
        System.out.println();
    }
}
}

Why is it that it prints out
BreezeEasyAs
As
NoProblem
LikeNothing
phew
I thought it would print the default after each one as in Breezephew for the first one

Upvotes: 0

Views: 116

Answers (3)

user2586804
user2586804

Reputation: 321

It starts at 1 and it's going up in 3s (x+=3).

So you get case 1 then case 4, 7, 10, 13, 16

But you don't always have breaks so it falls through to the next case in some cases.

Which is why case 1 actually gives "BreezeEasyAs", it runs case 1,2,3 & 4 before it catches a break.

Upvotes: 1

hevi
hevi

Reputation: 2716

Here are some points regarding switch-case structures you may benefit;

  • switch-case is a special structure replacing if-else in some cases.

  • In major programming languages (java, c++) only primitives, enumerators could be switched.

  • case represents nothing but an anchor (or so called label) and for that reason only primitives are used since the jump label has to be known during compile-time.

  • As case keywork actually represents a jump point, doesn't cause branching like if-else statements.

  • Switch does the jumping part (just like goto used in some old programming languages)

  • As it works by jumping the execution point, you have to put break keyword at the point you believe you are done with the case

  • If you don't put break execution will continue.

  • default keyword represents all of the case which are not handled, and is usually used for detecting unhandled cases

in your case consider that your are incrementing x by 3 and there is no default. lets consider x = 7; in this case execution will jump to case 7: and continue execution until it reaches a break thus executes everything until case 9 as there is the next break.

Upvotes: 0

user2720864
user2720864

Reputation: 8161

for the first time when
x = 1 it prints Breeze
but as there is no break in statement it keep on printing
Easy & As so befor it reach the end System.out.println(); you will get BreezeEasyAs in the console and then it runs in similar way ( incremented every time by +3)

so the next time :
x = 4 it prints As find the break and again incremented by 3
when
x = 7 it prints No finds no break and prints Problem ( so the console output says NoProblem) hope you can figure out the rest

Upvotes: 0

Related Questions