Here is an example code for a switch statement that checks both age and height in JavaScript:
index.tsx336 chars17 lines
Explanation:
switch
statement is used to execute different code based on different conditions.true
as the condition to evaluate.case
statement checks for a specific condition and executes a corresponding block of code if the condition is true
.break
statement is used to control the flow of the switch statement; it stops the execution of the switch block.default
statement is executed if none of the conditions match.In the code above, we first define two variables, age
and height
. Then, we use the switch
statement to check the values of age
and height
.
The switch statement checks each case
statement in sequence until it finds a matching one. If none of the case
statements match, it goes to the default
statement.
In this example, if the age is less than 18, it prints "You are under 18 years old". If the height is less than 150, it prints "You are under 150 cm tall". If both age is greater than or equal to 18 and height is greater than or equal to 150, it prints "You are eligible". If none of these conditions match, the default
statement is executed and it prints "Invalid input".
gistlibby LogSnag