JavaScript: Control Structures I

Slides:



Advertisements
Similar presentations
Types of selection structures
Advertisements

1 Outline Chapter 4 Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators.
 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.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
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.
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.
 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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
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.
 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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1 Outline 4.1Introduction 4.2Algorithms 4.3Pseudocode 4.4Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.
 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.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
1 Lecture 3 Control Structures else/if and while.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
 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.
Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
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.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
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 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 14 - JavaScript/JScript: Control Structures I
Chapter 9 - JavaScript: Control Statements II
Algorithm: procedure in terms of
Control Structures Sequential execution Transfer of control
while Repetition Structure
Chapter 13 - JavaScript/JScript: Introduction to Scripting
JavaScript: Control Statements I
Chapter 8 - JavaScript: Control Statements I
Chapter 15 - JavaScript/JScript: Control Structures II
Ch 7: JavaScript Control Statements I.
Chapter 4 – Control Structures Part 1
JavaScript: Control Statements (II)
JavaScript: Arrays.
Chapter 11 - JavaScript: Arrays
Chapter 8 - JavaScript: Control Structures I
Chapter 9 - JavaScript: Control Structures II
JavaScript: Functions
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 – Control Structures
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 8 - JavaScript: Control Structures I
Chapter 10 - JavaScript: Functions
Presentation transcript:

JavaScript: Control Structures I

Control Structures Flowcharting JavaScript’s sequence structure.

Control Structures

if/else Selection Structure Flowcharting the single-selection if structure.

if/else Selection Structure false true grade >= 60 print “Failed” print “Passed” Flowcharting the double-selection if/else structure.

Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) true product <= 1000 false Flowcharting the while repetition structure.

Prompt for the user input a grade. 1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 8.7: average.html --> 6 <!-- Class Average Program --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Class Average Program</title> 11 12 <script type = "text/javascript"> 13 <!-- 14 var total, // sum of grades 15 gradeCounter, // number of grades entered 16 gradeValue, // grade value 17 average, // average of all grades 18 grade; // grade typed by user 19 20 // Initialization Phase 21 total = 0; // clear total 22 gradeCounter = 1; // prepare to loop 23 24 // Processing Phase 25 while ( gradeCounter <= 10 ) { // loop 10 times 26 27 // prompt for input and read grade from user 28 grade = window.prompt( "Enter integer grade:", "0" ); 29 30 // convert grade from a string to an integer 31 gradeValue = parseInt( grade ); 32 33 // add gradeValue to total 34 total = total + gradeValue; 35 Average.html The while loop will execute the statements in the body of the loop until the value of gradeCounter equals 10. Prompt for the user input a grade. Convert input to an integer. Add new grade to total.

Average.html Program Output 36 // add 1 to gradeCounter 37 gradeCounter = gradeCounter + 1; 38 } 39 40 // Termination Phase 41 average = total / 10; // calculate the average 42 43 // display average of exam grades 44 document.writeln( 45 "<h1>Class average is " + average + "</h1>" ); 46 // --> 47 </script> 48 49 </head> 50 <body> 51 <p>Click Refresh (or Reload) to run the script again<p> 52 </body> 53 </html> Increment the counter. Average.html Program Output Calculate the average of the grades input by the user. Write the result to the XHTML document.

Prompt for the user to enter a grade, -1 to end. 1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 8.9: Average2.html --> 6 <!-- Sentinel-controlled Repetition --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Class Average Program: 11 Sentinel-controlled Repetition</title> 12 13 <script type = "text/javascript"> 14 <!-- 15 var gradeCounter, // number of grades entered 16 gradeValue, // grade value 17 total, // sum of grades 18 average, // average of all grades 19 grade; // grade typed by user 20 21 // Initialization phase 22 total = 0; // clear total 23 gradeCounter = 0; // prepare to loop 24 25 // Processing phase 26 // prompt for input and read grade from user 27 grade = window.prompt( 28 "Enter Integer Grade, -1 to Quit:", "0" ); 29 30 // convert grade from a string to an integer 31 gradeValue = parseInt( grade ); 32 33 while ( gradeValue != -1 ) { 34 // add gradeValue to total 35 total = total + gradeValue; Average2.html Prompt for the user to enter a grade, -1 to end. The while loop will continue until the user input equals –1.

36 37 // add 1 to gradeCounter 38 gradeCounter = gradeCounter + 1; 39 40 // prompt for input and read grade from user 41 grade = window.prompt( 42 "Enter Integer Grade, -1 to Quit:", "0" ); 43 44 // convert grade from a string to an integer 45 gradeValue = parseInt( grade ); 46 } 47 48 // Termination phase 49 if ( gradeCounter != 0 ) { 50 average = total / gradeCounter; 51 52 // display average of exam grades 53 document.writeln( 54 "<h1>Class average is " + average + "</h1>" ); 55 } 56 else 57 document.writeln( "<p>No grades were entered</p>" ); 58 // --> 59 </script> 60 </head> 61 62 <body> 63 <p>Click Refresh (or Reload) to run the script again</p> 64 </body> 65 </html> Average2.html Each iteration of the loop will open a prompt dialog allowing the user to input another grade.

Program Output

1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 8.11: analysis.html --> 6 <!-- Analyzing Exam Results --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Analysis of Examination Results</title> 11 12 <script type = "text/javascript"> 13 <!-- 14 // initializing variables in declarations 15 var passes = 0, // number of passes 16 failures = 0, // number of failures 17 student = 1, // student counter 18 result; // one exam result 19 20 // process 10 students; counter-controlled loop 21 while ( student <= 10 ) { 22 result = window.prompt( 23 "Enter result (1=pass,2=fail)", "0" ); 24 25 if ( result == "1" ) 26 passes = passes + 1; 27 else 28 failures = failures + 1; 29 30 student = student + 1; 31 } 32 Analysis.html The while loop will continue until the value of student is 10 meaning 10 results were entered. Entering a 1 into the prompt dialog means the student passed the exam. A value of 2 means the student failed. If a value of 1 was entered, the value of passes is incremented by one, otherwise, failures is incremented.

33 // termination phase 34 document.writeln( "<h1>Examination Results</h1>" ); 35 document.writeln( 36 "Passed: " + passes + "<br />Failed: " + failures ); 37 38 if ( passes > 8 ) 39 document.writeln( "<br />Raise Tuition" ); 40 // --> 41 </script> 42 43 </head> 44 <body> 45 <p>Click Refresh (or Reload) to run the script again</p> 46 </body> 47 </html> Analysis.html If more than 8 students passed the exam, the program says to “Raise Tuition”.

Program Output

Increment and Decrement Operators

1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 8.14: increment.html --> 6 <!-- Preincrementing and Postincrementing --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Preincrementing and Postincrementing</title> 11 12 <script type = "text/javascript"> 13 <!-- 14 var c; 15 16 c = 5; 17 document.writeln( "<h3>Postincrementing</h3>" ); 18 document.writeln( c ); // print 5 19 // print 5 then increment 20 document.writeln( "<br />" + c++ ); 21 document.writeln( "<br />" + c ); // print 6 22 23 c = 5; 24 document.writeln( "<h3>Preincrementing</h3>" ); 25 document.writeln( c ); // print 5 26 // increment then print 6 27 document.writeln( "<br />" + ++c ); 28 document.writeln( "<br />" + c ); // print 6 29 // --> 30 </script> 31 32 </head><body></body> 33 </html> Increment.html Postincrementing the variable will print the variable and then increment the value by one. Preincrementing the variable will increment the value by one and then print the value.

Program Output

Note on Data Types

JavaScript: Control Structures II

1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.1: WhileCounter.html --> 6 <!-- Counter-Controlled Repetition --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Counter-Controlled Repetition</title> 11 12 <script type = "text/javascript"> 13 <!-- 14 var counter = 1; // initialization 15 16 while ( counter <= 7 ) { // repetition condition 17 document.writeln( "<p style = \"font-size: " + 18 counter + "ex\">XHTML font size " + counter + 19 "ex</p>" ); 20 ++counter; // increment 21 } 22 // --> 23 </script> 24 25 </head><body></body> 26 </html> whileCounter.html The while loop will continue until the value of counter is greater than 7. Increment the counter.

Program Output

ForCounter.html Initialization Repetition condition Incrementing 1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.2: ForCounter.html --> 6 <!-- Counter-Controlled Repetition with for structure --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Counter-Controlled Repetition</title> 11 12 <script type = "text/javascript"> 13 <!-- 14 // Initialization, repetition condition and 15 // incrementing are all included in the for 16 // structure header. 17 for ( var counter = 1; counter <= 7; ++counter ) 18 document.writeln( "<p style = \"font-size: " + 19 counter + "ex\">XHTML font size " + counter + 20 "ex</p>" ); 21 // --> 22 </script> 23 24 </head><body></body> 25 </html> ForCounter.html Initialization Repetition condition Incrementing

Program Output

9.3 For Repetition Structure keyword Control variable name Final value of control variable for which the condition is true for ( var counter = 1 ; counter <= 7 ; ++counter ) Initial value of control variable Increment of control variable Loop-continuation condition Components of a typical for structure header.

9.4 Examples Using the for Structure Establish initial value of control variable. var counter = 1 document.writeln( true "<p style=\"font-size: " counter <= 7 ++counter + counter + Increment "ex\">XHTML font size " + the control false counter + "ex</p>" ); variable. Body of loop Determine if final value (this may be many statements) of control variable has been reached. Flowcharting a typical for repetition structure.

Sum.html Program Output 1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.5: Sum.html --> 6 <!-- Using the for repetition structure --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Sum the Even Integers from 2 to 100</title> 11 12 <script type = "text/javascript"> 13 <!-- 14 var sum = 0; 15 16 for ( var number = 2; number <= 100; number += 2 ) 17 sum += number; 18 19 document.writeln( "The sum of the even integers " + 20 "from 2 to 100 is " + sum ); 21 // --> 22 </script> 23 24 </head><body></body> 25 </html> Sum.html Program Output The for loop will continue until the value of number is greater than 100. Initialization. Repetition condition. Incrementing.

Interest.html Opening table element. 1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 9.6: interest.html --> 6 <!-- Using the for repetition structure --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>Calculating Compound Interest</title> 11 12 <script type = "text/javascript"> 13 <!-- 14 var amount, principal = 1000.0, rate = .05; 15 16 document.writeln( 17 "<table border = \"1\" width = \"100%\">" ); 18 document.writeln( 19 "<caption>Calculating Compound Interest</caption>" ); 20 document.writeln( 21 "<thead><tr><th align = \"left\">Year</th>" ); 22 document.writeln( 23 "<th align = \"left\">Amount on deposit</th>" ); 24 document.writeln( "</tr></thead>" ); 25 26 for ( var year = 1; year <= 10; ++year ) { 27 amount = principal * Math.pow( 1.0 + rate, year ); 28 document.writeln( "<tbody><tr><td>" + year + 29 "</td><td>" + Math.round( amount * 100 ) / 100 + 30 "</td></tr>" ); 31 } 32 33 document.writeln( "</tbody></table>" ); 34 // --> 35 </script> Interest.html Opening table element. Each iteration of the for loop creates a table row listing the year of the loan and the amount.

Interest.html Program Output 36 37 </head><body></body> 38 </html> Interest.html Program Output

Program Output