INFO/CSE 100, Spring 2005 Fluency in Information Technology

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM) INFO/CSE 100, Fall.
1 The Information School of the University of Washington Oct 30fit review Programming Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
The Information School of the University of Washington Oct 23 fit functions 1 Functions / If Else INFO/CSE 100, Fall 2006 Fluency in Information.
Looping Yong Choi School of Business CSU, Bakersfield.
29 November JavaScript: Arrays and Iterations Functions.
Oct 25 1 The Information School of the University of Washington fit control © 2006 University of Washington Control Flow INFO/CSE 100, Fall 2006.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Today’s Announcements Assignment 8 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Language Find the latest version of this document at
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
GoldSim Monthly Webinar Series. Goals  Learn the basics of simple scripts  Learn the basics of the GoldSim Script Element  Not a lesson on numerical.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Document Object Model (DOM)
Chapter 6: Loops.
Repetition Structures Chapter 9
Loops in Java.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Python: Control Structures
Control Structures.
Document Object Model (DOM)
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Document Object Model (DOM)
Repetition-Counter control Loop
JavaScript: Control Statements.
Incrementing ITP © Ron Poet Lecture 8.
The structure of computer programs
INFO/CSE 100, Spring 2005 Fluency in Information Technology
LOOPS.
Control Structures - Repetition
Document Object Model (DOM)
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Arrays, For loop While loop Do while loop
Looping and Repetition
Iteration with While You can say that again.
JavaScript Selection Statement Creating Array
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 8 JavaScript: Control Statements, Part 2
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Document Object Model (DOM)
INFO/CSE 100, Spring 2006 Fluency in Information Technology
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Control Structures: for & while Loops
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Logical Operations In Matlab.
Chapter 6: Repetition Statements
Loop Strategies Repetition Playbook.
Chapter 5: Control Structures II (Repetition)
INFO/CSE 100, Spring 2005 Fluency in Information Technology
FLUENCY WITH INFORMATION TECNOLOGY
The <script> Tag
Question 1a) What is printed by the following Java program? int s;
MIS 3200 – Unit 5.1 Iteration (looping) Working with List Items
Programming Basics Review
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Programming Fundamental
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 13 Control Structures
Presentation transcript:

INFO/CSE 100, Spring 2005 Fluency in Information Technology Control Flow INFO/CSE 100, Spring 2005 Fluency in Information Technology http://www.cs.washington.edu/100 4/17/2019 fit100-13-control © 2004 University of Washington

Readings and References Fluency with Information Technology Chapter 21, Iteration Principles 4/17/2019 fit100-13-control © 2004 University of Washington

if statement in Simple Sample GUI <script type="text/javascript"> function setResults(resultString) { var tempString = resultString; if (document.getElementById("radioLC").checked) { tempString = tempString.toLowerCase(); } else if (document.getElementById("radioUC").checked) { tempString = tempString.toUpperCase(); } document.getElementById("resultField").value = tempString; </script> the setResults(string) function is called by several event processors in every case, it takes the string that it is given, decides if upper or lower case is desired, and sets the resultField accordingly 4/17/2019 fit100-13-control © 2004 University of Washington

fit100-13-control © 2004 University of Washington The if / else statement The if statement is a conditional statement a conditional expression is evaluated as being true or false the expression is a boolean expression (ie, returns true or false) if the condition is true, then one set of statements is executed if the statement is false, then a different set of statements is executed if (<boolean expression>) { <statements> } else { } 4/17/2019 fit100-13-control © 2004 University of Washington

fit100-13-control © 2004 University of Washington Examples if (count == 0) { ready = false; } else { ready = true; count = count-1; } What is the conditional expression? What statements are part of the true block? Which statements are part of the false block? What happens when count is 21? 0? -1? if (pageCount >= 100) { alert("This may take a few minutes."); } What is the conditional expression? What statements are part of the true block? Which statements are part of the false block? What happens when pageCount is 21? 100? 200? 4/17/2019 fit100-13-control © 2004 University of Washington

scratch.html

W3Schools TryIt Editor

A Fancier Example of a GUI program a single tall latte, what a great way to start the morning 4/17/2019 fit100-13-control © 2004 University of Washington

An if statement from bean.html <head> <title>Interactive Coffee Cost Calculator</title> <script type="text/javascript"> function refresh() { var shotCount; // number of espresso shots var cupSize; // size of the cup in ounces var drink; // name of the requested drink var price; // calculated price of the drink var taxRate = 0.087; // Seattle retail tax var element; // the current gui element (radio button) for (var i=0; i<document.getElementById("shotForm").elements.length; i++) { element = document.getElementById("shotForm").elements[i]; if (element.checked) { shotCount = parseInt(element.value,10); } ... 4/17/2019 fit100-13-control © 2004 University of Washington

fit100-13-control © 2004 University of Washington Iteration Iteration or looping is a way to execute a block of program statements more than once we will use the for statement to create loops The for loop is generally controlled by counting There is an index variable that you increment or decrement each time through the loop When the index reaches some limit condition, then the looping is done and we continue on in the code 4/17/2019 fit100-13-control © 2004 University of Washington

Why do we want loops in our code? Do something for a given number of times or for every object in a collection of objects for every radio button in a form, see if it is checked for every month of the year, charge $100 against the balance calculate the sum of all the numbers in a list etc. Many loops are counting loops they do something a certain number of times 4/17/2019 fit100-13-control © 2004 University of Washington

The for loop A counting loop is usually implemented with for update loop control index shorthand for i=i+1 initialize check for limit for (var i=0; i < count; i++) { document.writeln("<br>index value is : "+i); } one or more statements in the loop body 4/17/2019 fit100-13-control © 2004 University of Washington

for example

fit100-13-control © 2004 University of Washington i++ is a shortcut for (i=0; i < count; i++) at the end of every pass through the for loop body, do the following: get the value of i increment i store the incremented value Used as it is here, this is the same as writing i = i + 1 4/17/2019 fit100-13-control © 2004 University of Washington

body of loop may not execute at all Notice that depending on the values of the control variables, it is quite possible that the body of the loop will not execute at all check for limit condition itemCount is 0 when we get here, so i<itemCount is immediately false and the loop body is skipped completely var itemCount = 0; ... for (var i=0; i < itemCount; i++) { document.writeln("<br>..processing item "+i); } 4/17/2019 fit100-13-control © 2004 University of Washington

fit100-13-control © 2004 University of Washington loop body skip 4/17/2019 fit100-13-control © 2004 University of Washington

fit100-13-control © 2004 University of Washington “Off By 1” Error The most common error when working with iterations is to miscount by 1 Everyone makes this mistake A common place where the “off by 1” error matters is in how many times a loop loops One advantage of a simple loop control statement is that it's easier to tell how many loops there will be for ( i=0; i<n; i++) { <statement list> } Number of iterations 4/17/2019 fit100-13-control © 2004 University of Washington

Another Example from the iCCC a double tall Cappuccio, what a great way to start the afternoon 4/17/2019 fit100-13-control © 2004 University of Washington

A for loop from bean.html <head> <title>Interactive Coffee Cost Calculator</title> <script type="text/javascript"> function refresh() { var shotCount; // number of espresso shots var cupSize; // size of the cup in ounces var drink; // name of the requested drink var price; // calculated price of the drink var taxRate = 0.087; // Seattle retail tax var element; // the current gui element (radio button) for (var i=0; i<document.getElementById("shotForm").elements.length; i++) { element = document.getElementById("shotForm").elements[i]; if (element.checked) { shotCount = parseInt(element.value,10); } ... 4/17/2019 fit100-13-control © 2004 University of Washington

fit100-13-control © 2004 University of Washington arrays On the previous page, we are selecting one element from a collection of elements this collection is an array named elements one entry for each radio button in the shotForm the length of this array is available document.getElementById("shotForm").elements.length we retrieve an individual element using the index variable element = document.getElementById("shotForm").elements[i]; The index of the first element is 0 4/17/2019 fit100-13-control © 2004 University of Washington