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

Slides:



Advertisements
Similar presentations
Control Structures Selections Repetitions/iterations
Advertisements

Decisions If statements in C.
More on Algorithms and Problem Solving
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 3 - Structured Program Development
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
Lecture 3 Structured Program Development in C
The University of Texas – Pan American
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
2440: 211 Interactive Web Programming Expressions & Operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Computer Organization Six logical units in every.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 14 - JavaScript/JScript: Control Structures I
Chapter 7 JavaScript: Control Statements, Part 1
Algorithm: procedure in terms of
- Standard C Statements
JavaScript: Control Statements I
Control Statements: Part 1
JavaScript: Control Statements I
Chapter 2.1 Control Structures (Selection)
Chapter 8 - JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Ch 7: JavaScript Control Statements I.
Programming Fundamentals
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 – Control Structures
Chapter 3 - Structured Program Development
Structural Program Development: If, If-Else
Presentation transcript:

/ 281 Internet Applications Ahmed M. Zeki Sem – / 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.

/ 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.

/ 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.

/ 285 All programs can be written in terms of only three control structures sequence structure (Built into JavaScript) selection structure repetition structure JavaScript doesnt have a goto statement. (unconditional branch).

/ 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.

/ 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.

/ 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.

/ 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).

/ 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: breakcasecontinue deletedoelse Falseforfunction ifinnew Nullreturnswitch thistruetypeof Varvoidwhile with

/ 2811 Keywords that are reserved by not used by JavaScript: catchclassconst debuggerdefaultenum exportextendsfinally emportsupertry 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.

/ 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).

/ 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.

/ 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.

/ 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. Example Document.writeln(grade>=60 ? Passed : Failed);

/ 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.

/ 2817 pseducode If students grade is greater than or equal to 90 print A Else if students grade is greater than or equal to 80 print B else if students grade is greater than or equal to 70 print C else if students grade is greater than or equal to 60 print D else print F

/ 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);

/ 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);

/ 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 ); document.writeln( You must take this course again. ); }

/ 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.

/ 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.

/ 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.

/ 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

/ 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.

/ 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.

/ 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.

/ 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: