ITSE 1411 Beginning Web
JS Decision Module 3 Discussion
Discussion
- JavaScript Decisions
- The "if" statements
- The "if" statement is a conditional statement that evaluates a condition to determine "if" the condition is true.
- When the condition is true the statement (or block of statements contained in curly braces) is executed.
- The syntax for the "if" statement is:
if (conditional expression)
statement;
or
if (conditional expression)
{
0 or more statements
}
- Parts of the "if" statement
- The keyword "if"
- The conditional statement enclosed in parenthesis. There is no semicolon after the conditional statement.
- The command block that is executed when the conditional statement is true.
The command block can be 0 to many statements. When there is only 1 statement,
the statement can stand-alone. In all other cases, the block must be contained in
curly braces.
- When the command block is complete, control goes to the next statement following the command block.
- When the conditional statement evaluates to false, the command block is skipped and control goes to the
next statement following the control block.
- The "if...else" statement
- The "if...else" statement begins the same as the "if" statement. It is a conditional statement that evaluates a condition to
determine "if" the condition is true.
- When the condition is true, the statement (or block of statements contained in curly braces) is executed.
- When the condition is false, the statement (or block of statements contained in curly braces) below the keyword else is executed.
- Parts of the "if...else" statement
- Starts the same as the "if" statement with the keyword "if"
- The conditional statement enclosed in parenthesis
- The command block with the exact same restrictions as the "if" statement.
- The 4th and 5th parts differ from the "if" statement. The 4th part is the keyword "else".
- A command block follows the "else" keyword with the same restrictions as the command block for the "if".
- When the conditional statement evaluates to true, the command block following the conditional statement is executed.
When the conditional statement evaluates to false, the command block following the keyword "else" is executed.
- After the appropriate command block is executed, control goes to the next statement following the command block after
the "else" keyword.
- The window.confirm() method as a conditional expression
- The window.confirm() method is like the "if...else" statement except that the user answer "true" or "false" in a dialog box.
- The syntax for the window.confirm() method is:
window.confirm(message);
The message can be either a literal string or a variable.
- When the user clicks the "OK" button, a true value is returned.
- When the user clicks the "Cancel" button, a false value is returned.
- When nothing is to occur when the "Cancel" button is clicked, the window.confirm() is treated like the "if" statement.
- Nested "if" and "if...else" statements
- Sometimes the response to a conditional statement requires an additional condition to be tested. This is called nested "if"
or nested "if...else" statements.
- "switch" statements
- The switch statement compares the value of an expression to another value.
- The switch statement uses the keywords switch and case.
- The syntax of the switch statement is
switch (expression)
{
case label: statement(s);
case label: statement(s);
...
default: statement(s);
}
- Different computer languages have different rules for the switch statement. If you know a different computer language
be careful when coding in JavaScript due to the differences.
- In JavaScript, the "label" is a specific value that can be literal values (string, numeric, or float), or a variable
that can be of type string, numeric, or float.
- When the value of an expression matches the "label" in the "case label", all statements following the matched value are executed until
either the end of the switch statement is reached or a break; statement is executed.
- When no "label" matches the value of the expression, the default is to match with the "default" label. When there is no "default" label
and no label matches, no statements are executed within the switch statement.
- The comparison is completed top to bottom until a "label" matches. The "default" label when used must be the last "label" coded within
the switch statement.