Chapter 4: Decision Making with Control Structures and Statements - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Chapter 4: Decision Making with Control Structures and Statements

Description:

Previewing CartoonQuiz.html File. Section A: Decision Making. Objectives ... The process of determining the order in which statements execute in a program is ... – PowerPoint PPT presentation

Number of Views:186
Avg rating:3.0/5.0
Slides: 25
Provided by: informat736
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4: Decision Making with Control Structures and Statements


1
Chapter 4 Decision Making with Control
Structures and Statements
  • JavaScript - Introductory

2
Previewing CartoonQuiz.html File
3
  • Section A
  • Decision Making

4
Objectives
  • In this section, you will learn about
  • if statements
  • if else statements
  • Nested if statements
  • Switch statements

5
If Statements
  • 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
  • Syntax for the if statement is if
    (conditional expression) statement(s)

6
If Statements
  • The if statement contains three parts key word
    if, a conditional expression enclosed within
    parentheses, and executable statements
  • The statement immediately following the if
    statement in Figure 4-2 can be written on the
    same line as the if statement itself

Examples\Example4_2.html
7
If Statements
  • Command block refers to multiple statements
    contained within a set of braces
  • When an if statement contains a command block,
    the statements in the block execute when the if
    statements condition evaluates to true
    (condition is satisfied)
  • Now, if the condition evaluates to false, both
    statements will be bypassed, since they are
    contained within a command block

8
If Statements
Examples\Example4_3.html
Examples\Example4_4.html
9
Comparison and Logical Operators with the If
Statement
Examples\Example4_5.html
10
Output of CartoonQuiz1.html
Examples\CartoonQuiz1.html
11
If Else Statements
  • When using an if statement, include an else
    clause to run an alternate set of code if the
    conditional expression evaluated by the if
    statement returns a value of false
  • An if statement that includes an else clause is
    called an if else statement
  • Consider the else clause a backup plan
  • An if statement can be constructed without the
    else clause. However, the else clause can only
    be used with an if statement

12
Nested If and IfElse Statements
  • An if statement contained within an if or if
    else statement is called a nested if statement
  • Similarly, an ifelse statement contained within
    an if or if else statement is called a nested if
    else statement
  • Use nested if and ifelse statements to perform
    conditional evaluations in addition to the
    original conditional evaluation (for
    example var numbr 7 if (number
    gt5) if (number lt 10)
    documnt.writeIn ( The number is between t
    and 10.)

13
Greeting Program with Nested If Statements
Examples\Example4_8.html
14
Modified Greeting Program with Nested If
Statements
Examples\Example4_9.html
15
Switch Statements
  • A switch statement controls program flow by
    executing a specific set of statements, depending
    on value of an expression
  • The switch statement compares the value of an
    expression to a label contained within a switch
    statement
  • If the value matches a particular label, then the
    statements associated with the label execute
  • The labels within a switch statement are called
    case labels and mark specific code segments

16
Examples of Case Labels
17
Switch Statements
  • A Case label consists of keyword case, followed
    by a literal value or variable name, followed by
    a colon
  • A case label can be followed by a single
    statement or multiple statements
  • Default label contains statements that execute
    when the value returned by a switch statements
    conditional expression does not match a case
    label keyword is default followed by a colon
  • Break statement is used to exit switch statements
    and other program control statements

18
Function Containing a Switch Statement
Examples\Example4_11.html
19
Greeting Program Using a Switch Statement
Examples\Example4_12.html
20
Section A Chapter Summary
  • Flow control is the process of determining the
    order in which statements are executed 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 refers to multiple statements
    contained within a set of braces
  • After an if statements condition evaluates true,
    either first statement following condition or
    command block following condition executes

21
Section A Chapter Summary
  • Statements following an if statements command
    or command block execute regardless of whether if
    statements conditional expression evaluates true
    or false
  • The else clause runs an alternate set of code if
    the conditional expression evaluated by an if
    statement returns a value of false
  • In an ifelse construct, only one set of
    statements executes either statement following
    if statement or statements following the else
    clause

22
Section A Chapter Summary
  • An if statement contained within another if
    statement is called a nested if statement
  • The switch statement controls program flow by
    executing a specific set of statements depending
    on the value returned by an expression
  • Case labels within a switch statement mark
    specific code segments

23
Section A Chapter Summary
  • A Default label contains statements that execute
    when the value returned by switch statements
    conditional expression does not match a case
    label
  • When a switch statement executes, the value
    returned by the conditional expression is
    compared to each case label in the order in which
    it is encountered
  • A break statement is used to exit a switch
    statement

24
  • Lets Practice This Stuff!!!!
Write a Comment
User Comments (0)
About PowerShow.com