JavaScript Loops Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
ISP 121 Algorithmsand Computer Programming. Why Know Simple Programming?  You can create powerful macros in Access / Excel / Word / ??? to manipulate.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Introduction to JavaScript Please see speaker notes for additional information!
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Computer Science 1620 Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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…
Advanced Decisions and Loops Chapter Some Simple Schoolroom Statistics.
Python quick start guide
 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.
Loops Doing the same thing over and over and over and over and over and over…
JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
Logic Structure - focus on looping Please use speaker notes for additional information!
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Variety of JavaScript Examples Please use speaker notes for additional information!
 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.
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
JavaScript, Fourth Edition
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Loops.  (No Quiz)  Hand in Assignment #1  Last chance for Q+A on the midterm  Loops 2.
Review while loops Control variables Example Infinite loop
Dynamic Web Authoring JavaScript – Looping statements COM311H Zheng, School of C&M, UUJ1.
Functions (doing a calculation) Please use speaker notes for additional information!
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
Learning Javascript From Mr Saem
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
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.
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.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
CHAPTER 4 DECISIONS & LOOPS
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
BIT116: Scripting Loops.
Chapter 6: Conditional Statements and Loops
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Introduction to DOM.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
the captured notes and redid - hopefully it all works.
Lab5 PROGRAMMING 1 Loop chapter4.
Note the rights settings.
Programs. at the code at the site..
PROGRAM FLOWCHART Iteration Statements.
Loops.
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
Wrapup which is the document write that says the end.
Presentation transcript:

JavaScript Loops Please use speaker notes for additional information!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " This is the while loop... <!-- var data_input=prompt("How many times should I write the line?",0); ct=1; while (ct <= data_input) { document.write("This is number " + ct + " "); ct = ct + 1; } document.write(" " + "This is the end!!!"); //--> In this program we are taking it as data_input the number of lines that you want to print. We then set ct to 1. The ct will be used to count the lines we write. The while loop is done while ct is <= data_input. Inside the loop you write a line which includes the ct. You then add 1 to ct. When the add 1 to ct makes ct no longer <= data_input. At that point, control drops out of the while loop and does the document write which says This is the end!!!

ct=1; while (ct <= data_input) { document.write("This is number " + ct + " "); ct = ct + 1; } ct=1 ct<= data_input Document. write including ct ct=ct + 1 Y N This flowchart shows the while loop with the decision being made prior to entering the loop.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " This is the while loop... <!-- //The do...while executes the statements in the loop ones and then repeats the //the execution while the condition is true. This means the do...while executes //at least once. This is the difference between the do...while and the while that //we looked at earlier. The while evaulates the condition before executing so the //statements in the loop may never be executed. var data_input=prompt("How many times should I write the line?",0); ct=1; do { document.write("This is number " + ct + " "); ct = ct + 1; } while (ct <= data_input) document.write(" " + "This is the end!!!"); //-->

ct=1 ct<= data_input Document. write including ct ct=ct + 1 Y N ct=1; do { document.write("This is number " + ct + " "); ct = ct + 1; } while (ct <= data_input) This shows the loop where the loop is entered and the code execute and then the condition is checked to determine whether or not the loop should be executed again.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + ans + " "); } //--> i j The for loop initializes, states the condition to stay in the loop and gives the increment value.

As shown on the last slide, 5 was entered and this triggered the wrong answer response. Note that I have now had j run through 1, 2, 3 and now I am back to the outer loop and i has been incremented by 1and is now 2 and j has been reset to 1 because the j for loop was reached through dropdown.

On the last pass, both i and j are 3. Note that each time the I gets incremented, the j gets reset to 1. i j i j

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } //--> i j

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- // If the user presses cancel the input is null - this program tests for null // Break exits out of the closest loop - note that I then set i to 4 to end the outer // loop - some programmers would not choose this approach because they avoid this // kind of manipulation

for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (user_input != null) { if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } else { i=4; break; } //-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- // If the user presses cancel the input is null - this program tests for null // Break exits out of the closest loop - note that I then set endFlag to Y and // when I return to the outer loop I test to see if the endFlag is Y which // means I want to break out of that loop as well // Note also that null and empty are not the same thing = "" is empty

var endFlag = "N" for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (user_input != null) { if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } else { endFlag="Y"; break; } if (endFlag == "Y") { break; } //-->