Title: Chapter 4: Decision Making with Control Structures and Statements
1Chapter 4 Decision Making with Control
Structures and Statements
- JavaScript - Introductory
2Previewing CartoonQuiz.html File
3- Section A
- Decision Making
4Objectives
- In this section, you will learn about
- if statements
- if else statements
- Nested if statements
- Switch statements
5If 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)
6If 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
7If 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
8If Statements
Examples\Example4_3.html
Examples\Example4_4.html
9Comparison and Logical Operators with the If
Statement
Examples\Example4_5.html
10Output of CartoonQuiz1.html
Examples\CartoonQuiz1.html
11If 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
12Nested 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.)
13Greeting Program with Nested If Statements
Examples\Example4_8.html
14Modified Greeting Program with Nested If
Statements
Examples\Example4_9.html
15Switch 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
16Examples of Case Labels
17Switch 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
18Function Containing a Switch Statement
Examples\Example4_11.html
19Greeting Program Using a Switch Statement
Examples\Example4_12.html
20Section 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
21Section 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
22Section 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
23Section 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!!!!