ITSE 1411 Beginning Web
JS Repetition Module 3 Discussion
Discussion
- JavaScript Repetition
Coding that is executed as long as a condition is true is called repetition or a loop statement.
- "while" statements
- The "while" statement like the "if" statement is a conditional statement that evaluates a condition to determine
when a condition is true.
- When the condition is true the statement (or block of statements contained in curly braces) is executed and continues until
the condition evaluates to false.
- The syntax of the "while" statement is:
while (conditional expression)
statement;
or
while (conditional expression)
{
0 or more statements
}
- Parts of the "while" statement
- The keyword "while"
- 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 back to the top of the loop to the conditional statement.
- When the conditional statement evaluates to false, the command block is skipped and control goes to the
next statement following the control block.
- In order to leave the loop, the value of the conditional statement must evaluate to false, which means that value
must is usually changed within the command block if control is to leave the loop.
- The command block can have a conditional expression used with an "if" that contains another command block with a break statement to
get out of the loop other than the conditional expression used with the "while".
- "do...while" statements
- The "do...while" statement is similar to the "while" statement except that the command block associated with the loop is
executed before the conditional statement is evaluated.
- The "do...while" statement is called a post-test loop.
- The syntax for the "do...while" statement is:
do {
statement(s);
} while (conditional expression);
- The conditional expression in the "do...while" statement ends with a semicolon.
- The command block can have a conditional expression used with an "if" that contains another command block with a break statement to
get out of the loop other than the conditional expression used with the "do...while".
- "for" statements
- The "for" statements is another pre-test loop, like the "while" statement.
- The "for" statement differs from the "while" statement because in addition to the conditional expression
the "for" statement can include code that initializes a counter and updates the counter with each iteration.
- The syntax of the "for" statement is:
for (counter declaration and initialization; conditional expression; update statement)
{
statement(s);
}
- Steps of the "for" loop.
- The counter variable is declared and initialized when the "for" loop begins.
- The conditional expression is evaluated.
- When the conditional expression evaluates to true, the command block is executed followed
by the update statement.
- When the conditional expression evaluates to false, control moves to the statement below the command block.
- The command block can have a conditional expression used with an "if" that contains another command block with a break statement to
get out of the loop other than the conditional expression used with the "for".
- "continue" statements
- The "continue" statement can be used with the "while", "do...while", and "for" statements to restart a loop with a new
iteration.