jschap04 - Chapter 4: Decision Making with Control...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Previewing CartoonQuiz.html File
Background image of page 2
Section A: Decision Making
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Objectives In this section, students will learn about: if statements if …else statements Nested if statements Switch statements
Background image of page 4
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); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 6
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 statement’s condition evaluates to true Now, if the condition evaluates to false, both statements will be bypassed, since they are contained within a command block
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
If Statements
Background image of page 8
Comparison and Logical Operators with the If Statement
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Output of CartoonQuiz1.html
Background image of page 10
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
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Nested If and If…Else Statements An if statement contained within an if or if… else statement is called a nested if statement Similarly, an if…else statement contained within an if or if… else statement is called a nested if … else statement Use nested if and if…else statements to perform conditional evaluations in addition to the original conditional evaluation (for example: var numbr = 7; if (number >5) if (number < 10) documnt.writeIn ( “The number is between t and 10.”);
Background image of page 12
Greeting Program with Nested If Statements
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Modified Greeting Program with Nested If Statements
Background image of page 14
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
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/13/2011 for the course MIS 241 taught by Professor Tse during the Spring '11 term at Moraine Valley Community College.

Page1 / 54

jschap04 - Chapter 4: Decision Making with Control...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online