Chapter 4(b): Fundamentals of JavaScript

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Chapter 4: Control Structures I (Selection)
Algorithms 10 IST – Topic 6.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Repetition control structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Chapter 04 (Part III) Control Statements: Part I.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Tutorial 11 Working with Operators and Expressions
Fundamentals of Python: From First Programs Through Data Structures
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 12: How Long Can This Go On?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
CS-3432 Electronic Commerce Lecture – 13 Sikandar Shujah Toor
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
JavaScript, Fourth Edition
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
XP Tutorial 2 New Perspectives on JavaScript, Comprehensive1 Working with Operators and Expressions Creating a New Year’s Day Countdown Clock.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(7) JavaScript: Control Statements I.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Language Find the latest version of this document at
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Chapter 9 - JavaScript: Control Statements II
1-1 Logic and Syntax A computer program is a solution to a problem.
Lecture 7: Repeating a Known Number of Times
Think What will be the output?
Programming the Web using XHTML and JavaScript
Chapter 5: Loops and Files.
Chapter 5: Repetition Structures
Lecturer CS & IT Department UOS MBDIN
JavaScript: Control Statements I
Topics discussed in this section:
Web Programming– UFCFB Lecture 16
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
JavaScript: Control Statements (II)
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
The Web Wizard’s Guide To JavaScript
Computer Science Core Concepts
JavaScript: Control Statements II
CSE 206 Course Review.
JavaScript 101 Lesson 8: Loops.
Presentation transcript:

Chapter 4(b): Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators, Expressions, and Statements 4.6 The JavaScript math Object 4.7 Comparison Operators and Decision-Making Structures 4.8 Loop Structures 4.9 Using JavaScript to Change Values in Form Fields

Chapter 4.8: Loop (Repetition) Structures Loops provide a structured way to perform repetitive calculations. Count-controlled loops perform calculations a pre-defined number of times. Conditional loops perform calculations until, or as long as, some predefined condition is true.

Count-Controlled Loops for (counter=start; {expression based on high (or low) value of counter}; expression controlling incrementing (or decrementing) of counter} ) Document 4.7 (counter2.htm) <html> <head> <title>Counter</title> <script> var k; document.write("Here's a simple counter: "+"<br />"); for (k=0; k<=10; k++) document.write(k+"<br />"); </script> </head> <body> </body> </html>

More Count-Controlled Loops Document 4.8 (countdown2.htm) <html> <head> <title>Countdown</title> <script> var k; document.write("Start launch sequence!" +"<br />"); for (k=10; k>=0; k--) document.write(k+"<br />"); document.write("FIRE!!"); </script> </head> <body> </body> </html>

Conditional Loops Pre-test loops – may not be executed at all, depending on initial settings. while ( {logical expression} ) { {statements that result in changing the value of the pre-test logical expression} } Post-test loops – always executed at least once. do { {statements that result in changing the value of the post-test logical expression} } while ( {logical expression} );

The Elevator Problem Document 4.9 (gorilla1.htm, partial) <script language="javascript" type="text/javascript"> var totalWeight=0.,limitWeight=500.,maxWeight=500.; var newWeight; do { newWeight=Math.floor(Math.random()*(maxWeight+1)); if ((totalWeight + newWeight) <= limitWeight) { totalWeight += newWeight; document.write("New weight = " + newWeight + " total weight = " + totalWeight + "<br />"); newWeight=0.; } else document.write("You weigh " + newWeight + " lb. I'm sorry, but you can't get on."); } while ((totalWeight + newWeight) <= limitWeight); </script>

Newton's Square Root Algorithm Given a number n: 1. Make a guess for the square root of n. n/2 is a reasonable guess. 2. Replace g with (g + n/g)/2. 3. Repeat step 2 until the absolute difference between g*g and n is smaller than some specified value. Document 4.10 (newtonSqrt2.htm) <html> <head> <title>Newton's square root algorithm</title> <script language="javascript" type="text/javascript"> var n=parseFloat(prompt("Enter a positive number:")); var g=n/2; do { g = (g + n/g)/2.; } while (Math.abs(g*g-n) > 1e-5); alert(g+" is the square root of "+n+"."); </script> </head> <body> </body> </html>

Using JavaScript to Change Form Field Values The basic use for JavaScript is to access and change the values in <input /> elements used in forms "Event handlers" allow us to do this from within forms.

Pressure Calculation <form> Fill in elevation and sea-level pressure: <input type="text" name="elevation" value="0" size="8" maxlength="7" /> (m) <input type="text" name="sea_level_pressure" value="1013.25" size="8" maxlength="7" /> (mbar) <br /> <input type="button" name="Calculate" value="Click here to get station pressure:" onclick="result.value= parseFloat(sea_level_pressure.value)- parseFloat(elevation.value)/9.2;" /> <input type="text" name="result" value="1013.25" size="8" maxlength="7" /> (mbar)<br /> <input type="reset" value="Reset all fields." /> </form>

Rectangular Rule Integration How do we do it?

Rectangular Rule Integration Use pseudocode to develop the algorithm: INITIALIZE integral = 0 (Initialize the value to 0.) LOOP for i = 0 to n – 1, x = x0 + i•dx + dx/2 y=x•x (This could be any function of x.) integral = integral + y END LOOP ASSIGN integral = integral•dx

The code: Document 4.13 <html><head><title></title></head> <body> <h2>Rectangular Rule integration</h2> for f(x)=x<sup>2</sup> <form> x<sub>0</sub>: <input type="text" name="x0" value="1" /> <br /> x<sub>1</sub>: <input type="text" name="x1" value="3" /> <br /> <input type="button" value="Click here to integrate." onclick="var x,X0,X1,i,n=20,integral=0,y; //y=x*x X1=parseFloat(x1.value); X0=parseFloat(x0.value); dx=(X1-X0)/n; for(i=0; i<n; i++) { x=X0 + i*dx + dx/2; y=x*x; integral+=y; } result.value=integral*dx; "/> <input type="text" name="result" value="result" /></br /> </form></body></html>