The Web Warrior Guide to Web Design Technologies

The Web Warrior Guide to Web Design Technologies Chapter 15 Java. Script: Part III

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

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);

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.

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.

Decision Making • The syntax of a switch statement is: switch (expression) { case label: statement(s); break; … default: statement(s); }

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.

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.

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

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); }

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.

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);

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.

Repetition • The syntax for a for loop is as follows: for(counter declaration and initialization; condition; update statement) { statement(s); }

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.

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.
- Slides: 16