pedalpete
pedalpete

Reputation: 21546

switch statement not working, only first case triggers actions

I'm new to writing switch statements after learning about it yesterday.

For some reason, this isn't working.

checkCase(2);
checkCase(1);
checkCase(0);
function checkCase(priorityType){
switch(priorityType){
     case 2:
         print(priorityType);
         break;             
        case 1:
        print(priorityType);
            break;          
        case 0:
        print(priorityType);
        break;
     }
}

The 'alert(2)' is triggered, 1 and 0 are not. I've reversed the case 2: with case 1: and run the code again and 2 is once again triggered, 1 is not. I've also tried adding break; and continue; to the cases, but still nothing.

Why is that? what have I done wrong? ----------------------EDIT--------------------------- Lots of responses saying that I need to add 'break;' which I've now done to each line. Still no output. I've also changed 'alert' to 'print'. No difference.

-------------edit2----------------------- my bad, the 'break' is working now. not sure what was going on when I checked last. Maybe needed to restart ff.

Upvotes: 0

Views: 2987

Answers (3)

Babiker
Babiker

Reputation: 18798

You must break; after each case.

function checkCase(priorityType){
    switch(priorityType){
        case 2:
             alert(priorityType);
        break;         

        case 1:
            alert(priorityType);    
        break;        

        case 0:
            alert(priorityType);
        break;

        //my two cents
        default:
            alert("No intended code for"+priorityType);
     }
}

checkCase(2);
checkCase(1);
checkCase(0);

Upvotes: 3

Mike Samuel
Mike Samuel

Reputation: 120516

Two problems: Debugging using alert is problematic and you need break statements.

Try adding breaks and using print in the square free shell, and I think you'll see the proper result.

checkCase(2);
checkCase(1);
checkCase(0);
function checkCase(priorityType){
switch(priorityType){
     case 2:
         print(priorityType);               break;
        case 1:
        print(priorityType);            break;
        case 0:
        print(priorityType);   break;
     }
}

Upvotes: 2

user113716
user113716

Reputation: 322502

Because there are no break statements, once a matched case is found, it and the subsequent cases will fire.

So when you do:

checkCase(2);

You'll get:

alert(2);
alert(2);
alert(2);

When you do:

checkCase(1);

You'll get:

alert(1);
alert(1);

When you do:

checkCase(0);

You'll get:

alert(0);

If you were hoping to get:

alert(2);
alert(1);
alert(0);

You'd need to change your switch to include break; statements.

function checkCase(priorityType){ 
   switch(priorityType){
     case 2:
         alert(priorityType);
         break;           
     case 1:
        alert(priorityType);       
        break;     
     case 0:
        alert(priorityType);
        break;
     }
}

Upvotes: 1

Related Questions