if ( condition ) {
/ / code to execute if condition is true
}
For example, if you asked people to enter their favorite Harry Potter character, and their favorite matched yours, an alert box could pop up saying "Me too! Hermione is wicked awesome!"
The if...else statement is similar, but you can also add script to run if a condition is false:
if ( condition ) {
/ / code to execute if condition is true
}
else {
/ / code to execute if condition is false
}
So in my example, maybe if they enter someone other than Hermione, the alert box says "Meh. Hermione's way cooler."
The switch statement is for when you want to check for multiple values:
switch ( expression ) {
case label1:
/ / code to be executed if expression equals label1
break;
case label2:
/ / code to be executed if expression equals label2
break;
default:
/ / code to be executed if expression is different
/ / from both label1 and label2
}
Note: the break statement stops the flow of the switch statement from continuing its evaluation.
No comments:
Post a Comment