how to use switch statements code example

Example 1: 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 2: switch case in

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")) //Tea
console.log(whatToDrink("evening")) //Shake
console.log(whatToDrink("night"))   //Water
console.log(whatToDrink("daytime")) //Water

Example 3: switch statement

int x = Random.Range(0, 5);
switch(x){
case 0:
//preform action if x=0
break;
case 1:
//preform action if x=1
break;
//CONTINUE DOWN
}

Tags:

C Example