internet & world wide web how to program, 5/e · the part of a script in which a variable name...

49
Internet & World Wide Web How to Program, 5/e ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Upload: others

Post on 22-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

Internet & World Wide Web How to Program, 5/e

©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Page 2: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

Counter-controlled repetition requires name of a control variable

initial value of the control variable

the increment (or decrement) by which the control variable is modified each time through the loop

the condition that tests for the final value of the control variable to determine whether looping should continue

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 3: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

The double-quote character delimits the beginning and end of a string literal in JavaScript it cannot be used in a string unless it is preceded

by a \ to create the escape sequence \”

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 4: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

HTML5 allows either single quotes (') or double quotes (") to be placed around the value specified for an attribute

JavaScript allows single quotes to be placed in a string literal

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 5: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 6: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 7: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

for statement Specifies each of the items needed for counter-controlled repetition with a control

variable

Can use a block to put multiple statements into the body

If the loop’s condition uses a < or > instead of a <= or >=, or vice-versa, it can result in an off-by-one error

for statement header contains three expressions Initialization

Condition

Increment Expression

The increment expression in the for statement acts like a stand-alone statement at the end of the body of the forstatement

Place only expressions involving the control variable in the initialization and increment sections of a for statement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 8: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 9: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 10: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

The three expressions in the for statement are optional

The two semicolons in the for statement are required

The initialization, loop-continuation condition and increment portions of a forstatement can contain arithmetic expressions

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 11: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

The part of a script in which a variable name can be used is known as the variable’s scope

The “increment” of a for statement may be negative, in which case it is called a decrement and the loop actually counts downward

If the loop-continuation condition initially is false, the body of the for statement is not performed Execution proceeds with the statement following

the for statement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 12: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 13: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

Figure 8.5 uses the for statement to sum the even integers from 2 to 100.

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 14: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 15: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 16: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 17: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 18: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 19: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 20: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 21: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

JavaScript does not include an exponentiation operator Math object’s pow method for this purpose. Math.pow(x, y) calculates the value of x raised to the yth power.

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 22: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

switch multiple-selection statement Tests a variable or expression separately for each of the values it

may assume

Different actions are taken for each value

CSS property list-style-type Allows you to set the numbering system for a 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)

others

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 23: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

switch statement Consists of a series of case labels and an optional default case When control reaches a switch statement

The script evaluates the controlling expression in the parentheses Compares this value with the value in each of the case labels If the comparison evaluates to true, the statements after the case label

are executed in order until a break statement is reached

The break statement is used as the last statement in each case to exit the switch statement immediately

The default case allows you to specify a set of statements to execute if no other case is satisfied Usually the last case in the switch statement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 24: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

Each case can have multiple actions (statements)

Braces are not required around multiple actions in a case of a switch

The break statement is not required for the last case because program control automatically continues with the next statement after the switch

Having several case labels listed together (e.g., case 1: case 2: with no statements between the cases) executes the same set of actions for each case

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 25: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 26: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 27: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 28: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 29: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 30: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 31: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

do…while statement tests the loop-continuation condition after the loop

body executes

The loop body always executes at least once

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 32: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 33: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 34: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 35: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

break statement in a while, for, do…whileor switch statement Causes immediate exit from the statement

Execution continues with the next statement in sequence

break statement common uses Escape early from a loop

Skip the remainder of a switch statement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 36: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

continue statement in a while, for or do…while 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

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 37: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 38: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 39: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 40: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 41: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

Logical operators can be used to form complex conditions by combining simple conditions && (logical AND)

|| (logical OR)

! (logical NOT, also called logical negation)

The && operator is used to ensure that two conditions are both true before choosing a certain path of execution

JavaScript evaluates to false or true all expressions that include relational operators, equality operators and/or logical operators

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 42: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 43: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

The || (logical OR) operator is used to ensure that either or both of two conditions are true before choosing choose a certain path of execution

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 44: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 45: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

The && operator has a higher precedence than the || operator

Both operators associate from left to right.

An expression containing && or || operators is evaluated only until truth or falsity is known This is called short-circuit evaluation

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 46: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

! (logical negation) operator reverses the meaning of a condition (i.e., a true

value becomes false, and a false value becomes true)

Has only a single condition as an operand (i.e., it is a unary operator)

Placed before a condition to evaluate to true if the original condition (without the logical negation operator) is false

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 47: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 48: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

Most nonboolean values can be converted to a boolean true or false value

Nonzero numeric values are considered to be true

The numeric value zero is considered to be false

Any string that contains characters is considered to be true

The empty string is considered to be false

The value null and variables that have been declared but not initialized are considered to be false

All objects are considered to be true

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.

Page 49: Internet & World Wide Web How to Program, 5/e · The part of a script in which a variable name can be used is known as the variable’s scope The “increment” of a forstatement

©1992-2012 by Pearson Education, Inc. All

Rights Reserved.