 2008 Pearson Education, Inc. All rights reserved. 1 7 7 JavaScript: Control Statements I.

Slides:



Advertisements
Similar presentations
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 Control structures  Algorithm & flowchart  If statements  While statements.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
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.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
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
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 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.
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.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4: Control Structures: Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
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.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2008 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 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
Chapter 3 Structured Program Development in C Part II 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 7 JavaScript: Control Statements, Part 1
Algorithm: procedure in terms of
Control Statements: Part 1
JavaScript: Control Statements I
Chapter 8 - JavaScript: Control Statements I
Ch 7: JavaScript Control Statements I.
Chapter 4 – Control Structures Part 1
JavaScript: Control Statements.
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Presentation transcript:

 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I

 2008 Pearson Education, Inc. All rights reserved Algorithms Any computable 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 execute and the order in which the actions are to execute is called an algorithm Specifying the order in which statements are to be executed in a computer program is called program control

 2008 Pearson Education, Inc. All rights reserved Control Structures Sequential execution – Execute statements in the order they appear in the code Transfer of control – Changing the order in which statements execute All programs can be written in terms of only three control structures – sequence – selection – repetition

 2008 Pearson Education, Inc. All rights reserved. 4 Fig. 7.1 | Flowcharting JavaScript’s sequence structure. Indicates a portion of an algorithm

 2008 Pearson Education, Inc. All rights reserved Control Structures (Cont.) 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 structure because it selects or ignores a single action or group of actions. – The if…else statement performs an action if a condition is true and performs a different action if the condition is false. Double-selection structure because it selects between two different actions or group of actions. – The switch statement performs one of many different actions, depending on the value of an expression. Multiple-selection structure because it selects among many different actions or groups of actions.

 2008 Pearson Education, Inc. All rights reserved Control Structures (Cont.) JavaScript provides four repetition statements, namely, while, do … while, for and for … in. Keywords cannot be used as identifiers (e.g., for variable names).

 2008 Pearson Education, Inc. All rights reserved. 7 Fig. 7.3 | Flowcharting the single-selection if statement. If grade >= 60, execute this statement Otherwise

 2008 Pearson Education, Inc. All rights reserved if...else Selection Statement Allows the programmer to specify that different actions should be performed when the condition is true and when the condition is false.

 2008 Pearson Education, Inc. All rights reserved. 9 Fig. 7.4 | Flowcharting the double-selection if else statement. If the condition is met… This is executed Otherwise… This is executed

 2008 Pearson Education, Inc. All rights reserved if...else Selection Statement (Cont.) Nested if…else statements – Test for multiple cases by placing if…else statements inside other 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 statement expects only one statement 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

 2008 Pearson Education, Inc. All rights reserved while Repetition Statement while – Allows the programmer 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

 2008 Pearson Education, Inc. All rights reserved. 12 Fig. 7.5 | Flowcharting the while repetition statement. If this statement is true … Execute this statement… And evaluate if this statement is still true Break out of the cycle when the statement is false

 2008 Pearson Education, Inc. All rights reserved Formulating Algorithms: Counter- Controlled Repetition Counter-controlled repetition – Often called definite repetition, because the number of repetitions is known before the loop begins executing A total is a variable in which a script accumulates the sum of a series of values – Variables that store totals should normally be initialized to zero before they are used in a program A counter is a variable a script uses to count— typically in a repetition statement

 2008 Pearson Education, Inc. All rights reserved. 14 Fig. 7.6 | Pseudocode algorithm that uses counter-controlled repetition to solve the class-average problem. Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average

 2008 Pearson Education, Inc. All rights reserved. 15 Fig. 7.7 | Counter- controlled repetition to calculate a class average (Part 1 of 3). Stores the sum of grades Sets total to 0 Sets gradeCounter to 1 in preparation for the loop Continues the cycle until gradeCounter is greater than 10

 2008 Pearson Education, Inc. All rights reserved. 16 Fig. 7.7 | Counter- controlled repetition to calculate a class average (Part 2 of 3). Increments gradeCounter by 1 after each iteration of the loop

 2008 Pearson Education, Inc. All rights reserved. 17 Fig. 7.7 | Counter-controlled repetition to calculate a class average (Part 3 of 3).

 2008 Pearson Education, Inc. All rights reserved Formulating Algorithms: Sentinel- Controlled Repetition 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 the number of repetitions is not known in advance Choose a sentinel value that cannot be confused with an acceptable input value

 2008 Pearson Education, Inc. All rights reserved. 19 Fig. 7.8 | Sentinel-controlled repetition to solve the class-average problem. Initialize total to zero Initialize gradeCounter to zero Input the first grade (possibly the sentinel) While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the average Else Print “No grades were entered”

 2008 Pearson Education, Inc. All rights reserved. 20 Fig. 7.9 | Sentinel- controlled repetition to calculate a class average (Part 1 of 3). Set gradeCounter to 0 in preparation for the loop

 2008 Pearson Education, Inc. All rights reserved. 21 Fig. 7.9 | Sentinel- controlled repetition to calculate a class average (Part 2 of 3). Increments gradeCounter by 1 after each iteration of the loop Begin new control structure Execute if the condition in the if statement is not satisfied Executes until gradeValue equals -1

 2008 Pearson Education, Inc. All rights reserved. 22 Fig. 7.9 | Sentinel-controlled repetition to calculate a class average (Part 3 of 3).

 2008 Pearson Education, Inc. All rights reserved Formulating Algorithms: Nested Control Statements Control structures may be nested inside of one another

 2008 Pearson Education, Inc. All rights reserved. 24 Fig | Examination-results problem pseudocode. Initialize passes to zero Initialize failures to zero Initialize student to one While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter Print the number of passes Print the number of failures If more than eight students passed Print “Raise tuition”

 2008 Pearson Education, Inc. All rights reserved. 25 Fig | Examination- results calculation (Part 1 of 3). Outer control structure Start nested control structure Increment for while loop End nested control structure

 2008 Pearson Education, Inc. All rights reserved. 26 Fig | Examination- results calculation (Part 2 of 3). Additional control structure

 2008 Pearson Education, Inc. All rights reserved. 27 Fig | Examination-results calculation (Part 3 of 3).

 2008 Pearson Education, Inc. All rights reserved Assignment Operators JavaScript provides the arithmetic assignment operators +=, -=, *=, /= and %=, which abbreviate certain common types of expressions.

 2008 Pearson Education, Inc. All rights reserved. 29 Fig | Arithmetic assignment operators.

 2008 Pearson Education, Inc. All rights reserved Increment and Decrement Operators The increment operator, ++, and the decrement operator, --, increment or decrement 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, then incremented or decremented by 1.

 2008 Pearson Education, Inc. All rights reserved. 31 Fig | Increment and decrement operators.

 2008 Pearson Education, Inc. All rights reserved. 32 Fig | Preincrementing and postincrementing (Part 1 of 2). Prints value of c, then increments it Increments c, then prints its value

 2008 Pearson Education, Inc. All rights reserved. 33 Fig | Preincrementing and postincrementing (Part 2 of 2).

 2008 Pearson Education, Inc. All rights reserved Increment and Decrement Operators (Cont.) When incrementing or decrementing a variable in a statement by itself, the preincrement and postincrement forms have the same effect, and the predecrement and postdecrement forms have the same effect When a variable appears in the context of a larger expression, preincrementing the variable and postincrementing the variable have different effects. Predecrementing and postdecrementing behave similarly.

 2008 Pearson Education, Inc. All rights reserved. 35 Fig | Precedence and associativity of the operators discussed so far.