javascript tutorial 4 -decision making with control structures and statements1 tutorial 4 decision...

Post on 21-Dec-2015

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

1

Tutorial 4

Decision Making with Control Structures and Statements

Section A - Decision Making

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

2

Tutorial 4A Topics

• Section A - Decision Making– if statements– if…else statements– Nested if statements– switch statements

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

3

Decision Making

• Decision Making– Process of determining the order in

which statements execute in a program• AKA – flow control

– Decision-Making Structures• Special type of JavaScript statements used

for making decisions

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

4

Decision Making

• if Statements– Used to execute specific programming code if

the evaluation of a conditional expression returns a value of true

• “Do this or don’t do this”

– Syntax (3 primary parts)

if (conditional_expression) {statement(s);

}

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

5

Decision Making

• if Statements– Operation

• If the conditional expression is true, the statement(s) is/are executed

• Control then continues to subsequent code

– Command block• Multiple statements contained within a set

of braces (require curly braces)

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

6

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

7

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

8

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

9

Decision Making

• if Statements– Conditional Expression

• Can consist of:– Comparison operators– Logical operators– Combination of the two

• Must resolve to Boolean value

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

10

Decision Making

• if…else Statements– Used to execute one set of code if the evaluation

of a conditional expression returns a value of true, otherwise executes another set of code

• “Do this or do something else”

– Syntax

if (conditional_expression) {statement(s);

} else {statement(s);

}

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

11

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

12

Decision Making

• Nested if and if…else Statements– Nested statements

• Statements contained within other statements

– Syntax (variable)if (conditional_expression) {

statement(s);if

(conditional_expression) {

statement(s);}

} else {statement(s);

}

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

13

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

14

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

15

Decision Making

• switch Statements– Controls program flow by executing a specific set

of statements, depending on the value of an expression

– Syntax switch (expression) {case label1:

statement(s);break;case label2:statement(s);break;default:statement(s);

}

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

16

Decision Making

• switch Statements– Case labels

• Identify specific code segments• Can use a variety of data types as case labels

– Break statement• Used to exit switch statements

– Default label• Contains statements that execute when the

condition expression doesn’t match any of the case labels

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

17

JavaScript Tutorial 4 -Decision Making with Control Structures and Statements

18

top related