web programming ch. 7
TRANSCRIPT
-
7/27/2019 Web Programming Ch. 7
1/72
Internet & World Wide WebHow to Program, 5/e
1992-2010 by Pearson Education, Inc. All Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
2/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
3/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
4/72
Any computable problem can be solved byexecuting a series of actions in a specificorder
A procedure for solving a problem in terms of the actions to be executed, and the order in which the actions are to be executedis called an algorithm
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
5/72
Pseudocode An informal language that helps you develop
algorithms
Pseudocode is similar to everyday English; its
convenient and user friendly, although its not anactual computer programming language
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
6/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
7/72
Sequential execution Execute statements in the order they appear in the code
Transfer of control Changing the order in which statements execute
All scripts can be written in terms of only threecontrol statements sequence
selection
repetition
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
8/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
9/72
Flowchart A graphical representation of an algorithm or of a
portion of an algorithm
Drawn using certain special-purpose symbols, such
as rectangles, diamonds, ovals and small circles Symbols are connected by arrows called flowlines,
which indicate the order in which the actions of thealgorithm execute
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
10/72
In a flowchart that represents a completealgorithm, oval symbols containing the wordsBegin and End represent the start and the end ofthe algorithm.
In a flowchart that shows only a portion of analgorithm, the oval symbols are omitted in favor ofusing small circle symbols, also called connectorsymbols.
Perhaps the most important flowcharting symbol is
the diamond symbol, also called the decisionsymbol, which indicates that a decision is to bemade.
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
11/72
JavaScript provides three selection structures. The if statement either performs (selects) an action if a
condition is true or skips the action if the condition is false.
Called a single-selection statement because it selects or ignoresa single action or group of actions.
The ifelse statement performs an action if a condition istrue and performs a different action if the condition is false.
Double-selection statement because it selects between twodifferent actions or group of actions.
The switch statement performs one of many different
actions, depending on the value of an expression. Multiple-selection statement because it selects among many
different actions or groups of actions.
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
12/72
JavaScript provides four repetitionstatements, namely, while, dowhile, forand forin.
In addition to keywords, JavaScript has otherwords that are reserved for use by thelanguage, such as the values null, true andfalse, and words that are reserved forpossible future use.
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
13/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
14/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
15/72
Single-entry/single-exit control statementsmake it easy to build scripts.
Control statements are attached to oneanother by connecting the exit point of onecontrol statement to the entry point of thenext. Control-statement stacking.
There is only one other way controlstatements may be connected Control-statement nesting
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
16/72
The JavaScript interpreter ignores white-spacecharacters blanks, tabs and newlines used for indentation and vertical
spacing
A decision can be made on any expression thatevaluates to a value of JavaScripts boolean type(i.e., any expression that evaluates to true orfalse).
The indentation convention you choose should be
carefully applied throughout your scripts It is difficult to read scripts that do not use uniform spacing
conventions
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
17/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
18/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
19/72
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
20/72
Allows you to specify that different actionsshould be performed when the condition istrue and when the condition is false.
1992-2010 by Pearson Education, Inc. All
Rights Reserved.
-
7/27/2019 Web Programming Ch. 7
21/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
22/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
23/72
Conditional operator (?:) Closely related to the ifelse statementJavaScripts only ternary operatorit takes three
operands The operands together with the ?: operator form a
conditional expression The first operand is a boolean expression The second is the value for the conditional
expression if the boolean expression evaluates to
true Third is the value for the conditional expression if
the boolean expression evaluates to false
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
24/72
Nested ifelse statements Test for multiple cases by placing ifelse statements inside
other ifelse statements
The JavaScript interpreter always associates anelse with the previous if, unless told to dootherwise by the placement of braces ({})
The if selection statement expects only onestatement in its body To include several statements, enclose the statements in braces ({
and }) A set of statements contained within a pair of braces is called a
block
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
25/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
26/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
27/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
28/72
A logic error has its effect at execution time. A fatal logic error causes a script to fail and
terminate prematurely.
A nonfatal logic error allows a script tocontinue executing, but the script producesincorrect results.
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
29/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
30/72
while Allows you to specify that an action is to be
repeated while some condition remains true
The body of a loop may be a single statement or a
block Eventually, the condition becomes false and
repetition terminates
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
31/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
32/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
33/72
Counter-controlled repetition Often called definite repetition, because the
number of repetitions is known before the loopbegins executing
A totalis a variable in which a scriptaccumulates the sum of a series of values Variables that store totals should normally be
initialized to zero before they are used in a script
A counteris a variable a script uses tocounttypically in a repetition statement
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
34/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
35/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
36/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
37/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
38/72
-
7/27/2019 Web Programming Ch. 7
39/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
40/72
JavaScript represents all numbers as floating-point numbers in memory
Floating-point numbers often developthrough division
The computer allocates only a fixed amountof space to hold such a value, so the storedfloating-point value can only be anapproximation
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
41/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
42/72
Sentinel-controlled repetition Special value called a sentinel value (also called a
signal value, a dummy value or a flag value)indicates the end of data entry
Often is called indefinite repetition, because thenumber of repetitions is not known in advance
Choose a sentinel value that cannot beconfused with an acceptable input value
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
43/72
Top-down, stepwise refinement A technique that is essential to the development of well-
structured algorithms
Approach begins with pseudocode of the top, the statement thatconveys the scripts overall purpose
Divide the top into a series of smaller tasks and list them in theorder in which they need to be performedthe first refinement
Second refinement commits to specific variables
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
44/72
-
7/27/2019 Web Programming Ch. 7
45/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
46/72
-
7/27/2019 Web Programming Ch. 7
47/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
48/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
49/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
50/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
51/72
-
7/27/2019 Web Programming Ch. 7
52/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
53/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
54/72
-
7/27/2019 Web Programming Ch. 7
55/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
56/72
Control structures may be nested inside ofone another
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
57/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
58/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
59/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
60/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
61/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
62/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
63/72
JavaScript provides the arithmetic assignmentoperators +=, -=, *=, /= and %=, whichabbreviate certain common types ofexpressions.
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
64/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
65/72
The increment operator, ++, and thedecrement operator, --, increment ordecrement a variable by 1, respectively.
If the operator is prefixed to the variable, the
variable is incremented or decremented by 1,then used in its expression.
If the operator is postfixed to the variable,the variable is used in its expression, thenincremented or decremented by 1.
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
66/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
67/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
68/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
69/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
70/72
When incrementing or decrementing avariable in a statement by itself, thepreincrement and postincrement forms havethe same effect, and the predecrement and
postdecrement forms have the same effect When a variable appears in the context of a
larger expression, preincrementing thevariable and postincrementing the variable
have different effects. Predecrementing andpostdecrementing behave similarly.
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
71/72
1992-2010 by Pearson Education, Inc. AllRights Reserved.
-
7/27/2019 Web Programming Ch. 7
72/72