Day 2: Switch Statements
The switch statement evaluates an expression, matches the expression's value to a case clause, and executes the statements associated with that particular case.
Switch Statements
Here's a useful video on the topic:
A switch statement first evaluates its expression.
Then, it looks for the first case clause whose expression matches the same value as the result of the input expression (using strict comparison, ===).
Finally, it transfers control to that clause.
Syntax:
switch (expression) {
case value1:
//Statements executed when the result of expression matches value1
[break;]
case value2:
//Statements executed when the result of expression matches value2
[break;]
...
case valueN:
//Statements executed when the result of expression matches valueN
[break;]
default:
//Statements executed when none of the values match the value of the expression
[break;]
}
With each case label, there is an optional break statement to ensure that the program breaks out of the switch once the matching statement is performed and continues execution at the statement following the switch.
If the break is omitted, the program continues execution at the next statement within the switch statement.
Example
switch (day) { //day is an integer varying from 0 to 6.
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "Looking forward to the Weekend";
}
Task
You are given a variable, . Your task is to print:
- ONE
, if is equal to .
- TWO
, if is equal to .
- THREE
, if is equal to .
- FOUR
, if is equal to .
- FIVE
, if is equal to .
- SIX
, if is equal to .
- SEVEN
, if is equal to .
- EIGHT
, if is equal to .
- NINE
, if is equal to .
- PLEASE TRY AGAIN
, if is none of the above.
Note: Do not declare the variable ; it is declared inside our code checker. Use console.log to print statements to the console.
xxxxxxxxxx
//Do not declare variable num.
//Write your code below this line.