** IF statement **
if statement runs a block of code when a condition is true.
you can use
ifstatement anotherifstatement. - this is called nested if .
if else statement
- else statement runs a block of code when if condition is false
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
- here we have
else ifis use to specifie the new condition first is false.
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
*Terinary operator *
- shorthand of if else
() ? x : y
Switch statement
Switch statement select one or more code blocks to be executed
Switch is often used as a more readable alternative to many if.. else if...else statements, especially when dealing with multiple possible values
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
break- it break out the switch block.this will stop the execution inside the switch block.
default - keyword specifies a block of code to run if there is no case match.The default keyword is optional.
Top comments (0)