20
Lesson 4: Controlling Program Flow in JavaScript

Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4:Controlling Program Flow in JavaScript

Page 2: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Objectives

• Use the if... statement

• Use the while... statement

• Use the do...while statement

• Use the for... statement

• Use the break and continue statements

• Use the switch... statement

Page 3: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Controlling Decisional Program Flow

• Control the execution of JavaScript statements

• Control structure• In programming, a statement that uses a comparison

operator to make decisions based on Boolean values, or a statement that causes code to execute repeatedly (i.e., loop)

Page 4: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

The if...else Statement

• A single condition

• Multiple conditions

• Using if for conditional program flow

• Multiple conditions in the same expression

Page 5: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

The while Statement

• The while statement • Used to execute a block of code for as long as (while) a

certain test condition is true

• The isNaN method • Used to determine whether a given value is a valid

number

Page 6: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

The do...while Statement

• The do...while statement • Does not check the conditional expression until after the

first time through the loop, guaranteeing that the code within the curly braces will execute at least once

Page 7: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

The for Statement

• The for statement • Repeats a group of statements for some particular range

of values

Page 8: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

The break Statement

• The break statement• Used to exit a loop that would otherwise continue to

execute

Page 9: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

The continue Statement

• The continue statement • Used to force the flow of control back to the top of a loop

• Using continue in a while loop

Page 10: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

The switch Statement

• The switch statement • Compares a value against other values, searching for a

match

Page 11: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Summary

Use the if... statement

Use the while... statement

Use the do...while statement

Use the for... statement

Use the break and continue statements

Use the switch... statement

Page 12: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

1. Which JavaScript statement should you use to force the flow of control back to the top of a loop?

a. break

b. continue

c. switch

d. do...while

Page 13: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

1. Which JavaScript statement should you use to force the flow of control back to the top of a loop?

a. break

b. continue

c. switch

d. do...while

Page 14: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

2. Which JavaScript statement should you use to execute a block of code for as long as a certain test condition is true, with the guarantee that the code block will execute at least once?

a. do

b. do...while

c. if

d. if...else

Page 15: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

2. Which JavaScript statement should you use to execute a block of code for as long as a certain test condition is true, with the guarantee that the code block will execute at least once?

a. do

b. do...while

c. if

d. if...else

Page 16: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

3. Which JavaScript statement should you use to compare a value against other values when searching for a match?

a. while

b. if...else

c. for

d. switch

Page 17: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

3. Which JavaScript statement should you use to compare a value against other values when searching for a match?

a. while

b. if...else

c. for

d. switch

Page 18: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

4. Which JavaScript statement should you use to branch to one of two processes depending on the result of a test condition?

a. if...else

b. for

c. break

d. do...while

Page 19: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

4. Which JavaScript statement should you use to branch to one of two processes depending on the result of a test condition?

a. if...else

b. for

c. break

d. do...while

Page 20: Lesson 4: Controlling Program Flow in JavaScript•Use the switch... statement. Controlling Decisional Program Flow •Control the execution of JavaScript statements •Control structure

Lesson 4 Quiz

5. Write a for loop that outputs the following:

10

9

8

7

6

5

4

3

2

1

0