Download presentation
Presentation is loading. Please wait.
Published byLogan Gardner Modified over 9 years ago
1
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies
2
Objectives Use if statements and if…else statements to make decisions Nest one if statement in another Use switch statements to make decisions Use while and do…while statements to execute code repeatedly Use for statements to execute code repeatedly
3
Decision Making The process of determining the order in which statements execute in a program is called decision making or flow control. The if statement is used to execute specific programming code if the evaluation of a conditional expression returns a value of true: if (conditional expression) statement(s);
4
Decision Making A command block is a set of statements contained within a set of braces. The if…else statement is useful when a set of statements needs to be executed when a condition is true and another set of statements needs to be executed when a condition evaluates to false.
5
Decision Making The syntax of an if…else statement is: if (conditional expression) statement; else statement; When one decision-making statement is contained within another decision-making statement, they are referred to as nested decision-making structures.
6
Decision Making The syntax of a switch statement is: switch (expression) { case label: statement(s); break; case label: statement(s); break; … default: statement(s); }
7
Decision Making When a switch statement is executed, the expression in parentheses (the controlling expression) is evaluated and its value is compared with the values listed after the word case (the case labels). If a match is found, the statements after the matching case label are executed.
8
Decision Making A default label is used within a switch statement. The default label contains statements that executes when the value returned by the switch statement conditional expression does not match a case label. A break statement is used to end a switch statement once it has performed its required task.
9
Repetition A loop statement is a control structure that repeatedly executes a statement or a series of statements while a specific condition is true or until a specific condition becomes true. There are three types of loop statements: –while –do…while –for
10
Repetition The while statement is used for repeating a statement or series of statements as long as a given conditional expression evaluates to true. The syntax of a while statement is: while(conditional expression) { statement(s); }
11
Repetition Each repetition of a looping statement is called an iteration. A counter is a variable that increments or decrements with each iteration of a loop statement. An infinite loop is a situation in which a loop statement never ends because its conditional expression is never false.
12
Repetition The do…while statement executes a statement or a group of statements once and then repeats the execution for as long as a given conditional expression evaluates to true. The syntax is as follows: do { statement)s); } while (conditional expression);
13
Repetition The statements in a do…while loop execute at least once. The for statement is used for repeating a statement or series of statements as long as a given conditional expression evaluates to true.
14
Repetition The syntax for a for loop is as follows: for(counter declaration and initialization; condition; update statement) { statement(s); }
15
Summary Flow control is the process of determining the order in which statements execute in a program. The if statement is used to execute specific programming code if the evaluation of a conditional expression returns true. A command block is a set of statements contained within a set of braces, similar to the way function statements are contained within a set of braces. If...else statements execute an alternate set of code if the conditional expression evaluated by an if statement returns a value of false. The switch statement controls program flow by executing a specific set of statements, depending on the value returned by an expression.
16
Summary A loop statement repeatedly executes a statement or a series of statements as long as a specific condition is true or until a specific condition becomes true. The while statement is used for repeating a statement or series of statements as long as a given conditional expression evaluates to true. The do...while statement executes a statement or statements once and then repeats the execution as long as a given conditional expression evaluates to true. The for statement repeats a statement or series of statements as long as a given conditional expression evaluates to true.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.