Mohd Aqib
Mohd Aqib

Reputation: 158

Switch case program

This is a Swtich Case program. I am not sure why this happens in this program. Can anyone explain why this happening?

class SwitchCase{
     public static void main(String args[]){
         switch(3*4+2){
            case 12:
                System.out.println("Hello");
            case 14:
                System.out.println("Hello World");

            case 16:
                System.out.println("Hello World Aqib");
            case 18:
                System.out.println("Hello World Mohd");
            default:
                System.out.println("Hahah....");
            }
        }
  } 

 Output:
     Hello World 
     Hello World Aqib
     Hello World Mohd
     Hahah....

Upvotes: 0

Views: 36

Answers (1)

Roxana Roman
Roxana Roman

Reputation: 1012

After each case you need a break;

 case 12:
                System.out.println("Hello");
                break;
.....

Upvotes: 2

Related Questions