or condition in switch case code example

Example 1: javascript switch statement multiple cases

//javascript multiple case switch statement
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";
}

//darkOrLight="Light"

Example 2: java switch multiple values

switch (num) {
            case 5:
            case 2:
            case 3:
                System.out.println("5,2,3");
                break;
            case 4:
            case 6:
                System.out.println("4,6");
                break;
            case 1:
                System.out.println("1");
                break;
            default:
                System.out.println("Inavlid number");
                break;
        }

Example 3: 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

Tags:

C Example