281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

28
/ 28 1 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9

Transcript of 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

Page 1: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 281

Internet Applications

Ahmed M. Zeki

Sem – 2

2000/2001

----------------

Chapter 9

Page 2: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 282

Control Structure Any computing problem can be solved by

executing a series of actions in a specific order. A procedure for solving a problem in terms of the

actions to be executed and the order in which these actions are to be executed is called an algorithm.

Specifying the order in which statements are to be executed in a computer program is called program Control.

Page 3: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 283

PseudoCode is an artificial and informal language that helps programmers develop algorithms,

PseudoCode is not actually executed on computers. Rather, it helps the programmer "think out" a Program before attempting to write it in a programming language such as JavaScript.

Carefully prepared pseudocode may be converted easily to a corresponding JavaScript program.

Page 4: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 284

PseudoCode normally describes only executable statements-the actions that are performed when the program is converted from pseudocode to JavaScript and is run.

Normally, statements in a program are executed one after the other, in the order in which they are written. This is called sequential execution.

Various JavaScript statements enable the programmer to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control.

Page 5: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 285

All programs can be written in terms of only three control structures sequence structure (Built into JavaScript) selection structure repetition structure

JavaScript doesn’t have a goto statement. (unconditional branch).

Page 6: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 286

A flowchart is a graphical representation of an algorithm or of a portion of an algorithm. Flowcharts are drawn using certain special-purpose symbols, such as rectangles, diamonds, ovals and small circles; these symbols are connected by arrows called flowlines, which indicate the order in which the actions of the algorithm execute.

Flowcharts are often useful for developing and representing algorithms, although pseudocode is strongly preferred by many programmers.

Page 7: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 287

Oval symbol: contains the word “Begin” is the first symbol used in the flowchart.

Oval symbol: contains the word “End” indicates where the algorithm ends.

In a flowchart that shows only a portion of an algorithm the oval symbols are omitted in favor of using small circle symbols also called connector symbols.

The most important symbol is the diamond symbol also called decision symbol which indicates that a decision is to be made.

Page 8: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 288

JavaScript provides three types of selection structures. The if selection structure either performs (selects) an

action if a condition is true or skips the action if the condition is false.

The if/else selection structure performs an action if a condition is true and performs a different action if the condition is false.

The switch selection structure performs one of many different actions, depending on the value of an expression.

Page 9: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 289

The if structure is called a single-selection structure, because it selects or ignores a single action (or a single group of actions).

The if/else structure is called a double-selection structure, because it selects between two different actions (or groups of actions).

The switch structure is called a multiple-selection structure, because it selects among many different actions (or groups of actions).

Page 10: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2810

JavaScript provides four types of repetition structures, namely while, do/while, for and for/in.

Keywords cannot be used as identifiers (such as for variable names).

JavaScript Keywords:break case continuedelete do elseFalse for functionif in newNull return switchthis true typeofVar void while

with

Page 11: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2811

Keywords that are reserved by not used by JavaScript:catch class const debugger default enumexport extends finallyemport super try

Single-entry/single-exit control structures make it easy to build programs. Control structures are attached to one another by connecting the exit point of one control structure to the entry point of the next this is called control- structure stacking. There is only one other way control structures may be connected-control-structure nesting.

Page 12: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2812

Selection Structure A selection structure is used to choose among

alternative courses of action in a program. The JavaScript interpreter ignores whitespace

characters: blanks, tabs and newlines used for indentation and vertical spacing. Programmers insert these whitespace characters to enhance program clarity.

A decision can be made on any expression that evaluates to a value of JavaScript's Boolean type (any expression that evaluates to true or false).

Page 13: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2813

The if/else selection structure allows the programmer to specify that different actions are to be performed when the condition is true and when the condition is false.

The indentation convention you choose should be carefully applied throughout your programs. It is difficult to read programs that do not use uniform spacing conventions.

Page 14: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2814

In JavaScript, any non-zero numeric value in a condition evaluates to true, and 0 evaluates to false.

For strings, any string containing 1 or more characters evaluates to true and the empty string (the string containing no characters) evaluates to false.

Also, a variable that has been declared with var but has not been assigned a value evaluates to false.

Page 15: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2815

JavaScript provides an operator called the conditional operator (? : ) that is closely related to the if/else structure. 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 condition evaluates to true and the third is the value for the conditional expression if the condition evaluates to false.

ExampleDocument.writeln(grade>=60 ? “Passed” : “Failed”);

Page 16: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2816

Nested if/else structures test for multiple cases by placing if/else structures inside if/else structures.

The JavaScript interpreter always associates an else with the previous if unless told to do otherwise by the placement of braces ( { } ).

The if selection structure normally expects only one statement in its body. To include several statements in the body of an if, enclose the statements in braces ({and}). A set of statements contained within a pair of braces is called a compound statement.

Page 17: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2817

pseducodeIf student’s grade is greater than or equal to 90 print “A”Else if student’s grade is greater than or equal to 80 print “B” else if student’s grade is greater than or equal to 70 print “C” else if student’s grade is greater than or equal to 60 print “D” else print “F”

Page 18: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2818

If (Grade >= 90) document.writeln (“A”);Else if (Grade >=80) document.writeln (“B”); else if (Grade >=70) document.writeln (“C”); else if (Grade >=60) document.writeln (“D”); else document.writeln (“F”);

Page 19: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2819

It is important to note that the JavaScript interpreter always associates an else with the previous if, unless told to do otherwise by the placement of braces ({}). This is referred to as the dangling-else problem.

Example: If ( x>5 ) if (y > 5) document.writeln (“x and y are > 5”);

else document.writeln ( “x is <= 5”);

Page 20: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2820

Example: If ( x>5 ) { if (y > 5) document.writeln (“x and y are > 5”);}

else document.writeln ( “x is <= 5”);

Example: If ( grade >= 60 ) Document.writeln( “passed” );Else { document.writeln( “Failed<BR>” ); document.writeln( “You must take this course again.” );}

Page 21: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2821

A logic error has its effects at execution time. A fatal logic error causes a program to fail and terminated prematurely. A nonfatal logic error allows a program to continue executing, but the program produces incorrect results.

A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true.

Counter- controlled repetition is often called definite repetition, because the number of repetitions is known before the loop begins executing.

Page 22: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2822

Uninitialized variables used in mathematical calculations result in logic errors and produce the value NaN (not a number).

JavaScript represents all numbers as floating-point numbers in memory. Floating-point numbers often develop through division. The computer allocates only a fixed amount of space to hold such a value, so the stored floating-point value can only be an approximation.

Page 23: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2823

In sentinel-controlled repetition, a special value called a sentinel value (also called a signal value, a dummy value, a flag value or special code) indicates "end of data entry." Sentinel-controlled repetition is often called indefinite repetition, because the number of repetitions is not known in advance.

The sentinel value must be chosen so that It is not confused with an acceptable input value, such as –1.

Page 24: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2824

JavaScript provides the arithmetic assignment operators +=, -=, *=, /= and %= that help abbreviate certain common types of expressions:

+=, c += 1 is equivalent to c = c + 1 -=, c -= 1 is equivalent to c = c - 1 *=, c *= 2 is equivalent to c = c * 2 /=, c /= 2 is equivalent to c = c / 1 %=, c %= 2 is equivalent to c = c % 2

Page 25: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2825

The increment operator, ++, and the decrement operator, --, increment or decrement a variable by 1. If the operator is prefixed to the variable, the variable is incremented or decremented by 1 first, then used in its expression. If the operator is postfixed to the variable, the variable is used in its expression, then incremented or decremented by 1.

Page 26: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2826

++a: increment a by 1, then use the new value of a in the expression in which a resides.

a++: use the current value of a in the expression in which a resides, then increment a by 1.

--b: Decrement b by 1, then use the new value of b in the expression in which b resides.

b--: use the current value of b in the expression in which b resides, then decrement b by 1.

Page 27: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2827

JavaScript does not require variables to have a type before they can be used in a program. A variable in JavaScript can contain a value of any data type, and in many situations JavaScript automatically converts between values of different types for you. For this reason. JavaScript is referred to as a loosely type language.

When a variable is declared in JavaScript but is not given a value, that variable has an undefined value. Attempting to use the value of such a variable is normally a logic error.

Page 28: 281 Internet Applications Ahmed M. Zeki Sem – 2 2000/2001 ---------------- Chapter 9.

/ 2828

When variables are declared, they are not assigned default values unless specified otherwise by the programmer. To indicate that a variable does not contain a value. you can assign the value null to the variable.

Try: www.javascript.com www.javascriptmall.com