JavaScript Control Statements
JavaScript Control Statements
control flow
learning objectives:
At the end of the lesson, students will be able to:
else Statement
else if Statement
switch Statement
CONDITIONAL STATEMENTS
Use if to specify a block of code to be executed, if
a specified condition is true
Use else to specify a block of code to be
executed, if the same condition is false
Use else if to specify a new condition to test, if
the first condition is false
Use switch to specify many alternative blocks of
code to be executed
THE IF STATEMENT
Use the if statement to specify a block of JavaScript
code to be executed if a condition is true.
Syntax:
THE IF STATEMENT
Make a "Good day" greeting if the hour is less than 18:00:
THE ELSE STATEMENT
Use the else statement to specify a block of code to be
executed if the condition is false.
Syntax:
THE ELSE STATEMENT
If the hour is less than 18, create a "Good day" greeting, otherwise "Good
evening".
THE ELSE IF STATEMENT
Use the else if statement to specify a new condition if
the first condition is false.
Syntax:
THE ELSE IF STATEMENT
If time is less than 10:00, create a "Good morning" greeting, if not, but time is
less than 20:00, create a "Good day" greeting, otherwise a "Good evening":
SWITCH STATEMENT
NOTE:
If default is not the last case in
the switch block, remember to
end the default case with a
break.
SWITCH:COMMON CODE BLOCKS