xxxxxxxxxx
// The argument can be all sorts of different types. In this case it is an Int.
switch (Argument_Example){
case 0:
Debug.Log("Argument = 0");
break; // 'break' is used to mark the end of a section.
case 1:
Debug.Log("Argument = 1");
break;
case 2:
Debug.Log("Argument = 2");
break;
case 3:
Debug.Log("Argument = 3");
break;
case 4:
Debug.Log("Argument = 4");
break;
case 5:
Debug.Log("Argument = 5");
break;
}
xxxxxxxxxx
switch (expression) {
case condition 1: statement(s)
break;
case condition 2: statement(s)
break;
case condition n: statement(s)
break;
default: statement(s)
}
xxxxxxxxxx
var Animal = "Giraffe";
switch (Animal) {
case "Cow":
case "Giraffe":
case "Dog":
case "Pig":
console.log("This animal is not extinct.");
break;
case "Dinosaur":
default:
console.log("This animal is extinct.");
}
xxxxxxxxxx
function getFruitByColor(color) {
switch (color) {
case "red":
return "apple";
case "yellow":
return "banana";
case "orange":
return "orange";
case "green":
return "pear";
default:
return "grape";
}
}
const favoriteColor = "yellow";
const fruit = getFruitByColor(favoriteColor);
console.log("I want to eat a " + fruit);
xxxxxxxxxx
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
}
xxxxxxxxxx
int myValue;
switch myValue{
case 0:
doSomething();
case 1:
doSomethingElse();
default: //else
break; //Or call another function
}
xxxxxxxxxx
function caseInSwitch(val) {
switch (val) {
case 1:
return "alpha";
break;
case 2:
return "beta";
break;
case 3:
return "gamma";
break;
case 4:
return "delta";
break;
}
}
// Change this value to test
caseInSwitch(1);
xxxxxxxxxx
//The switch Statement
const day = prompt(`write a week day`);
switch (day) {
case "monday":
console.log("Go to gym in the morning");
console.log(`Come back home and spend time with my lovely wife`);
break;
case "tuesday":
console.log(`Sleep long with ZIZI`);
break;
case "wednesday":
case "thursday":
console.log(
`Get up and go in university with ZIZI and then take care of BAQSI`
);
break;
case "friday":
console.log(`Test some games in my work`);
break;
case "saturday":
case "sunday":
console.log(`Enjoy the weekend with ZIZI `);
break;
default:
console.log(`Not a valid day`);
}
xxxxxxxxxx
switch (new Date().getDay()) { // input is current day
case 6: // if (day == 6)
text = "Saturday";
break;
case 0: // if (day == 0)
text = "Sunday";
break;
default: // else...
text = "Whatever";
}
xxxxxxxxxx
switch (expression) {
case value1:
// Code to execute when expression equals value1
break;
case value2:
// Code to execute when expression equals value2
break;
// Additional cases as needed
default:
// Code to execute when none of the cases match
}