switch case statement code example
Example 1: js switch case
switch(expression) {
case x:
break;
case y:
break;
default:
}
Example 2: javascript switch statement multiple cases
var color = "yellow";
var darkOrLight="";
switch(color) {
case "yellow":case "pink":case "orange":
darkOrLight = "Light";
break;
case "blue":case "purple":case "brown":
darkOrLight = "Dark";
break;
default:
darkOrLight = "Unknown";
}
Example 3: switch statement in c
#include <stdio.h>
int main(void)
{
int a = 0;
switch(a)
{
case 1 :
statement("a = 1");
break;
case 2 :
printf("a = 2");
break;
default :
printf("a is neither 1 or 2");
break;
}
}
Example 4: switch case in javascript
function whatToDrink(time){
var drink ;
switch (time) {
case "morning":
drink = "Tea";
break;
case "evening":
drink = "Shake";
break;
default:
drink="Water";
}
return drink;
}
console.log(whatToDrink("morning"))
console.log(whatToDrink("evening"))
console.log(whatToDrink("night"))
console.log(whatToDrink("daytime"))
Example 5: switch function
var rand = Math.round(random(1,4));
switch(rand){
case 1: console.log("Chosen Number : 1");
break;
case 2: console.log("Chosen Number : 2");
break;
case 3: console.log("Chosen Number : 3");
break;
case 4: console.log("Chosen Number : 4");
default: break;
}
Example 6: java switch case
switch(x){
case(0):
break;
default:
break;
}