Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript: Control Statements I

Similar presentations


Presentation on theme: "JavaScript: Control Statements I"— Presentation transcript:

1 JavaScript: Control Statements I

2 Introduction JavaScript provides three types of selection structures.
The if selection (single-selection statement) statement performs (selects) an action if a condition is true or skips the action if the condition is false. The if…else (double-selection statement) selection statement performs an action if a condition is true and performs a different action if the condition is false. The switch (multiple-selection statement) selection statement performs one of many different actions, depending on the value of an expression. JavaScript provides four repetition statements—while, do…while, for and for…in.

3

4 if Selection Statement
A selection statement is used to choose among alternative courses of action in a script. For example: The preceding pseudocode If statement can be written in JavaScript as

5 if…else Selection Statement
The if…else selection statement allows you to specify that a different action is to be performed when the condition is true than when the condition is false. For example: The preceding pseudocode If…Else statement may be written in JavaScript as

6 Conditional Operator (?:)
The operator ?: is JavaScript’s only ternary operator—it takes three operands. The operands together with the ?: form a conditional expression. The first operand is a boolean expression, the second is the value for the conditional expression if the expression evaluates to true and the third is the value for the conditional expression if the expression evaluates to false.

7 Nested if...else Statements
Nested if…else statements test for multiple cases by placing if…else statements inside if…else statements.

8 Dangling-else Problem
The following code illustrates the dangling-else problem. For example, The interpreter interprets the preceding statement as To force the first nested if statement to execute as it was intended originally, we must write it as follows: The braces ({}) indicate to the JavaScript interpreter that the second if statement is in the body of the first if statement and that the else is matched with the first if statement.

9 while Repetition Statement
A repetition structure (also known as a loop) allows you to specify that a script is to repeat an action while some condition remains true. The statement is as follows:

10 Assignment Operators JavaScript provides several additional assignment operators (called compound assignment operators) for abbreviating assignment expressions. Examples Any statement of the form where operator is one of the binary operators +, -, *, / or %

11 Increment and Decrement Operators

12

13 Formulating Algorithms: Counter-Controlled Repetition
Consider the following problem statement:

14

15 Formulating Algorithms: Sentinel-Controlled Repetition
Consider the following problem: Develop a class-averaging script that will process an arbitrary number of grades each time the script is run.

16

17 Nested Control Statements

18

19

20 Essentials of Counter-Controlled Repetition
Counter-controlled repetition requires: The name of a control variable (or loop counter). The initial value of the control variable. The increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop). The condition that tests for the final value of the control variable to determine looping should continue.

21 for Repetition Statement

22 General Format of a for Statement

23 Summing Integers with a for Statement

24 Calculating Compound Interest with the for Statement
Consider the following problem statement:

25

26 switch Multiple-Selection Statement

27

28 We create these different lists using the CSS property list-style-type, which allows us to set the numbering system for the list. Possible values include decimal (numbers—the default), lower-roman (lowercase Roman numerals), upper-roman (uppercase Roman numerals), lower-alpha (lowercase letters), upper-alpha (uppercase letters), and more.

29 do…while Repetition Statement
In the while statement, the loop-continuation test occurs at the beginning of the loop, before the body of the loop executes. The do…while statement tests the loop-continuation condition after the loop body executes—therefore, the loop body always executes at least once.

30

31 break and continue Statements
break Statement The break statement, when executed in a while, for, do…while or switch statement, causes immediate exit from the statement. Execution continues with the first statement after the structure.

32 continue Statement The continue statement, when executed in a while, for or do…while statement, skips the remaining statements in the body of the statement and proceeds with the next iteration of the loop. In while and do…while statements, the loop-continuation test evaluates immediately after the continue statement executes. In for statements, the increment expression executes, then the loop- continuation test evaluates. Improper placement of continue before the increment in a while may result in an infinite loop.

33 Logical Operators JavaScript provides logical operators that can be used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical NOT, also called logical negation). && (Logical AND) Operator

34 || (Logical OR) Operator
! (Logical Negation) Operator


Download ppt "JavaScript: Control Statements I"

Similar presentations


Ads by Google