BIT116: Scripting Loops.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 5: Loops and Files.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 12: How Long Can This Go On?
30/10/ Iteration Loops Do While (condition is true) … Loop.
Variety of JavaScript Examples Please use speaker notes for additional information!
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
JavaScript Loops Please use speaker notes for additional information!
JavaScript, Fourth Edition
Loops.  (No Quiz)  Hand in Assignment #1  Last chance for Q+A on the midterm  Loops 2.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Controlling Program Flow with Looping Structures
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Popup Boxes.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 6 Controlling Program Flow with Looping Structures.
Decomposing A Program Into Functions. 2 3 Program/Function Decomposition.
BIT116: Scripting Lecture 05
ECE Application Programming
CHAPTER 10 JAVA SCRIPT.
BIT116: Scripting Lecture 06
REPETITION CONTROL STRUCTURE
ECE Application Programming
Week of 12/12/16 Test Review.
Introduction To Repetition The for loop
Topics Introduction to Repetition Structures
Loop Structures.
CS1371 Introduction to Computing for Engineers
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
While Loops Chapter 3.
Lecture 4B More Repetition Richard Gesick
HTML Forms and User Input
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Use proper case (ie Caps for the beginnings of words)
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Lesson 2: Input and Variables
T. Jumana Abu Shmais – AOU - Riyadh
Programming in JavaScript
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Coding Concepts (Basics)
Introduction to TouchDevelop
Due Next Monday Assignment 1 Start early
Additional Topics in VB.NET
M150: Data, Computing and Information
Looping III (do … while statement)
Programming in JavaScript
Blackboard Beginner Level Training
Web Programming Ben Blanc
Topics Introduction to Repetition Structures
BIT116: Scripting Radio Buttons.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops.
MIS 3200 – Unit 5.1 Iteration (looping) Working with List Items
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
EECE.2160 ECE Application Programming
MIS 3200 – Unit 6.1 Moving between pages by redirecting
Presentation transcript:

BIT116: Scripting Loops

Today NO QUIZ Enjoy the free points!  Hand in Assignment #1 Final Revision (only for those people who previously submitted a reflective essay) Hand in Assignment #2 Revision Loops And the accumulator pattern

Loops Let’s look at LoopsDemo.html

For this loop we'll first examine the code and then trace through it Loops example – while loop (indefinite loop) For this loop we'll first examine the code and then trace through it $("#loops").click( function() { 1) var output = ""; 2) var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run 3) while (userInput != null) { 4) userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); 5) if (userInput != null) { 6) output += userInput + "<br/>"; } 7) $("#whileLoopOutput").html( output ); First Step – set up the accumulator var output = ""; = create the variable, and make sure that it's empty Second step – set up the input variable var userInput = ""; = the important thing is that this is not null, so the loop will run at least once

Loops example – while loop (indefinite loop) $("#loops").click( function() { 1) var output = ""; 2) var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run 3) while (userInput != null) { 4) userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); 5) if (userInput != null) { 6) output += userInput + "<br/>"; 7) } } $("#whileLoopOutput").html( output ); Repeat the following: while (userInput != null) {…} = While the user has NOT clicked cancel userInput = prompt( ); = pop up the dialog box. When the user clicks cancel this will return the special value null. "What would you like to append next?\nClick 'cancel' to stop" = This is what to display in the box. Note that \n means "go to a new line here"

Loops example – while loop (indefinite loop) $("#loops").click( function() { var output = ""; var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run while (userInput != null) { userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); if (userInput != null) { output += userInput + "<br/>"; } $("#whileLoopOutput").html( output ); During each repetition do the following: if (userInput != null) { … } = Do this only if the user did NOT click cancel output += userInput + "<br/>"; = add the newest input onto the "accumulator variable". Put the "<br/>" at the end so that the input will be listed one per line (i.e., vertically)

Loops example – while loop (indefinite loop) $("#loops").click( function() { var output = ""; var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run while (userInput != null) { userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); if (userInput != null) { output += userInput + "<br/>"; } $("#whileLoopOutput").html( output ); Last Step for the while loop: $("#whileLoopOutput").html( output ); = display all the accumulated inputs onto the page

Loops example – while loop (indefinite loop) $("#loops").click( function() { 1) var output = ""; 2) var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run 3) while (userInput != null) { 4) userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); 5) if (userInput != null) { 6) output += userInput + "<br/>"; } 7) $("#whileLoopOutput").html( output ); Value of output 1 "" 2 (I'm going to assume that the user enters Mintthen Loop, and then clicks 'Cancel')

Loops example – while loop (indefinite loop) $("#loops").click( function() { 1) var output = ""; 2) var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run 3) while (userInput != null) { 4) userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); 5) if (userInput != null) { 6) output += userInput + "<br/>"; 7) } } $("#whileLoopOutput").html( output ); Value of output Value of userInput 3 "" 4 "Mint" 5 6 "Mint<br/>" (I'm going to assume that the user enters Mint then Loop, and then clicks 'Cancel')

"Mint<br/>Loop<br/>" Loops example – while loop (indefinite loop) Value of output Value of userInput 3 "" 4 "Mint" 5 6 "Mint<br/>" "Loop" "Mint<br/>Loop<br/>" (I'm going to assume that the user enters Mint then Loop, and then clicks 'Cancel') $("#loops").click( function() { 1) var output = ""; 2) var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run 3) while (userInput != null) { 4) userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); 5) if (userInput != null) { 6) output += userInput + "<br/>"; 7) } } $("#whileLoopOutput").html( output );

"Mint<br/>Loop<br/>" Loops example – while loop (indefinite loop) Value of output Value of userInput 3 "" 4 "Mint" 5 6 "Mint<br/>" "Loop" "Mint<br/>Loop<br/>" null (user clicked "Cancel") $("#loops").click( function() { 1) var output = ""; 2) var userInput = ""; // A while loop is good here because // we don't know how many times the loop will run 3) while (userInput != null) { 4) userInput = prompt("What would you like to append next?\nClick 'cancel' to stop"); 5) if (userInput != null) { 6) output += userInput + "<br/>"; 7) } } $("#whileLoopOutput").html( output );

Again, we'll first explain the code and then trace it Loops example – for loop (counting loop) Again, we'll first explain the code and then trace it // while loop stuff is left out, but in the example file it goes above here output = ""; // reset output to be blank // A for loop is good here because // we're counting the number of times the loop runs var i; for (i = 0; i < 10; i++) { output += "The current value of <b>i</b> is: " + i + "<br/>"; } $("#forLoopOutput").html( output ); First Steps for the for loop: output = ""; = reset the accumulator var i; = create a variable to hold the count

Loops example – for loop (counting loop) // while loop stuff is left out, but in the example file it goes above here output = ""; // reset output to be blank // A for loop is good here because // we're counting the number of times the loop runs var i; for (i = 0; i < 10; i++) { output += "The current value of <b>i</b> is: " + i + "<br/>"; } $("#forLoopOutput").html( output ); Repetition Steps: for (i = 0; i < 10; i++) {…} = start i at zero, count up by one each time, and stop when i is 10 output += "The current value of <b>i</b> is: " + i + "<br/>"; = glue the current value of i onto the accumulator string

Loops example – for loop (counting loop) // while loop stuff is left out, but in the example file it goes above here output = ""; // reset output to be blank // A for loop is good here because // we're counting the number of times the loop runs var i; for (i = 0; i < 10; i++) { output += "The current value of <b>i</b> is: " + i + "<br/>"; } $("#forLoopOutput").html( output ); Last Step $("#forLoopOutput").html( output ); = Display the output on the page

"The current value of <b>i</b> is: 0 <br/>" Loops example – for loop (counting loop) // while loop stuff is left out, but in the example file it goes above here 1) output = ""; // reset output to be blank // A for loop is good here because // we're counting the number of times the loop runs 2) var i; 3) for (i = 0; i < 10; i++) { 4) output += "The current value of <b>i</b> is: " + i + "<br/>"; 5) } 6) $("#forLoopOutput").html( output ); Value of i Value of output 1 Does not exist "" 2 Undefined 3 4 "The current value of <b>i</b> is: 0 <br/>" "The current value of <b>i</b> is: 0 <br/>The current value of <b>i</b> is: 1 <br/>"

(Continued from prior slide) Loops example – for loop (counting loop) // while loop stuff is left out, but in the example file it goes above here 1) output = ""; // reset output to be blank // A for loop is good here because // we're counting the number of times the loop runs 2) var i; 3) for (i = 0; i < 10; i++) { 4) output += "The current value of <b>i</b> is: " + i + "<br/>"; 5) } 6) $("#forLoopOutput").html( output ); Value of i Value of output (Continued from prior slide) 4 1 "The current value of <b>i</b> is: 0 <br/>The current value of <b>i</b> is: 1 <br/>" 3 2 "The current value of <b>i</b> is: 0 <br/>The current value of <b>i</b> is: 1 <br/>The current value of <b>i</b> is: 2 <br/>" Etc.

Do Exercises Work on Exercises #1 and #2 for this lecture

Good exam question: Describe (in your own words) when is a good situation to use a while (indefinite) loop? Given some code and a description of what the user does, show: What the contents of the string will be. Example: "Hi, I'm <b>Kermit</b>!<br/>" What will be displayed on the web page Example: Hi, I'm Kermit

Loops – break, continue

Loops example – for loop (counting loop) $("#loops").click( function() { var output = ""; var userInput = ""; while (userInput != null) { userInput = prompt("What would you like to append next?" + "\nClick 'cancel' to stop" + "\nType STOP to stop, or IGNORE to have your input ignored"); if (userInput != null) { if (userInput == "STOP") { break; // out of the nearest enclosing loop } if (userInput == "IGNORE") { continue; // jump straight back to line 14 // & start the next iteration of the loop output += userInput + "<br/>"; At this point you should understand the first two lines of code  while (userInput != null) { … } = repeat ALL of this until the user clicks on cancel

1 Loops example – for loop (counting loop) $("#loops").click( function() { var output = ""; var userInput = ""; while (userInput != null) { userInput = prompt("What would you like to append next?" + "\nClick 'cancel' to stop" + "\nType STOP to stop, or IGNORE to have your input ignored"); if (userInput != null) { if (userInput == "STOP") { break; // out of the nearest enclosing loop } if (userInput == "IGNORE") { continue; // jump straight back to line 14 // & start the next iteration of the loop output += userInput + "<br/>"; The code next to the will display the prompt box over the web page if (userInput != null) { = this will be true if the user clicked on "Ok" (and NOT "cancel") 1 1

Loops example – for loop (counting loop) $("#loops").click( function() { var output = ""; var userInput = ""; while (userInput != null) { userInput = prompt("What would you like to append next?" + "\nClick 'cancel' to stop" + "\nType STOP to stop, or IGNORE to have your input ignored"); if (userInput != null) { if (userInput == "STOP") { break; // out of the nearest enclosing loop } if (userInput == "IGNORE") { continue; // jump straight back to line 14 // & start the next iteration of the loop output += userInput + "<br/>"; if (userInput == "STOP") { = If the user typed STOP exactly (must be all caps) break; = Then break out of the loop (jump to , and continue after there) break break

Loops example – for loop (counting loop) $("#loops").click( function() { var output = ""; var userInput = ""; while (userInput != null) { userInput = prompt("What would you like to append next?" + "\nClick 'cancel' to stop" + "\nType STOP to stop, or IGNORE to have your input ignored"); if (userInput != null) { if (userInput == "STOP") { break; // out of the nearest enclosing loop } if (userInput == "IGNORE") { continue; // jump straight back to line 14 // & start the next iteration of the loop output += userInput + "<br/>"; if (userInput == "IGNORE") { = If the user typed STOP exactly (must be all caps) continue; = Then stop this time through the loop, jump to , and continue from there (and, of course, show the output on the screen) continue continue

Do Exercises Work on Exercise #3 for this part of this lecture