xxxxxxxxxx
function caseInSwitch(val) {
let answer = "";
// Only change code below this line
switch (val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
break;
}
// Only change code above this line
return answer;
}
// Change this value to test
caseInSwitch(1);
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
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
}
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
#include <iostream>
int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
default:
std::cout << "Invalid day" << std::endl;
}
return 0;
}