ITM 352 - © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8.

Slides:



Advertisements
Similar presentations
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Advertisements

COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
ITM352 Loops Lecture #6. 1/28/03ITM352 Fall 2003 Class 5 – Control Flow 2 Announcements r Lab class dates have changed!!! m Check the “Weekly Schedule”
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CS303ELoops1 CS 303E Lecture 8: Loops Self-referentially, short for GNU's not UNIX, a Unix-compatible software system developed by the Free Software Foundation.
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.
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4 – PHP Flow Control SFDV3011 – Advanced Web Development 1.
Flow of Control Chapter 3 Flow of control Branching Loops
CS101 Computer Programming I Chapter 4 Extra Examples.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
 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.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Fundamental Programming Fundamental Programming More on Repetition.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
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()
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2016, Fred McClurg, All Rights.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
JavaScript: Control Statements.
ITM 352 Flow-Control: Loops
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
Web Programming– UFCFB Lecture 20
2.6 The if/else Selection Structure
Presentation transcript:

ITM © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8

ITM © Port, Kazman2 Iteration Summary of loops while-do for do-while  avoid using this! while loop examples for loop examples

ITM © Port, Kazman3 Iteration Traditional method of repeating statements (not using recursion) loop control { } repeat these statements zero or more times, controlled by loop control

ITM © Port, Kazman4 Iteration (cont.) Three different forms of iteration while for do-while

ITM © Port, Kazman5 While loops while ( condition ) statement contains variables that are changed in loop repeat until condition becomes false Keep in mind that statement can be a compound or block statement.

ITM © Port, Kazman6 For loops for ( init ; cond ; incr ) Statement init ; //for condition while ( cond ) { Statement incr ; }  Mostly for convenience and readability

ITM © Port, Kazman7 Do-while loops do Statement while ( cond ) Statement ; while ( cond ) Statement 

ITM © Port, Kazman8 While loops Syntax: while ( cond ) statement What it means: Test cond. If false, then while loop is finished; go on to next statement If true, execute statement, then go back and start again Continue like this until cond becomes false or a break is executed.

ITM © Port, Kazman9 More While loop Examples $x = 5; $s = 0; while ($x > 0) { $s = $s + $x; $x = $x-1; }

ITM © Port, Kazman10 More While loop Examples $s = 0; $x = 1; while ($x <= 10) { $s = $s + $x; $x = $x + 1; }

ITM © Port, Kazman11 Infinite and Empty Loops while (true); is an infinite loop and you will have to wait a long time! while (false){//stuff} and while ($n++ < 10); {//stuff} are empty loops and will never execute anything You should take care to ensure your loop is good by (1)Checking that the initial condition is true (2)Checking that the loop condition will eventually change to false (3)Do NOT EVER use ; after while() (4)Make sure you initialize loop variables BEFORE the loop Generally PHP has a maximum execution time to help avoid crashing the system (default 60 secs). You can set this to a lower value such as 5 sec by using set_time_limit (5);

ITM © Port, Kazman12 While loop examples while (true) echo "I will do the reading assignments"; while (false) echo "I will start my HW early"; Output? Do Lab Exercise 1

ITM © Port, Kazman13 The exit Function If you have a situation where it is pointless to continue execution you can terminate the program with the exit() or die() functions A string is often used as an argument to identify if the program ended normally or abnormally

ITM © Port, Kazman14 exit,die example $x = 5; while ($x++>0) { // terminate program at 3 if($x == 3) die(“done!”); echo “$x ”; } echo “I will not execute  ”;

ITM © Port, Kazman15 break and continue If you have a situation where need to exit the loop but not terminate the program use break If want to stop executing the statements in the loop for a given cycle and immediately jump to the top of the loop to start the next cycle use continue

ITM © Port, Kazman16 break example $x = 5; while ($x++>0) { // stop early at 3 if($x == 3) break; echo “$x ”; } echo “I will always execute”;

ITM © Port, Kazman17 continue example $x = 5; while ($x++>0) { // skip 3 if($x == 3) continue; echo “$x ”; } echo “I will always execute”; Do Lab Exercise 2

ITM © Port, Kazman18 for loops Convenient syntax for loops with loop variables that are incremented or decremented each time through the loop (as in previous examples). Recall: for ( init ; cond ; incr ) statement init ; while ( cond ) { statement incr ; } 

ITM © Port, Kazman19 for loop examples Previous examples: $s = 0; $x = 5; while ($x > 0) { $s = $s + $x; $x = $x-1; } $s = 0; for ($x = 5; $x > 0; $x = $x-1) $s = $s + $x; 

ITM © Port, Kazman20 for loop examples $s = 0; for ($x = 1; $x <= 10; $x = $x+1) $s = $s + $x;  $s = 0; $x = 1; while ($x <= 10) { $s = $s + $x; $x = $x + 1; }

ITM © Port, Kazman21 for loop usage for ($i = 1; $i <= n ; $i++) statement One use of for loops is simply to execute a statement, or sequence of statements, a fixed number of times: executes statement exactly n times.

ITM © Port, Kazman22 for loop usage (cont.) for ($i = 1; $i <= 100; $i++) echo "I will not turn HW in late"); Example: print "I will not turn HW in late" a hundred times.

ITM © Port, Kazman23 for loop usage (cont.) for ($i = 0; $i < 20; $i++) { if ($i%2 == 1) echo "My robot loves me\n"; else echo "My robot loves me not\n"; } Keep in mind that loop bodies can be as complicated as you like. This loop prints: "My robot loves me" and "My robot loves me not" on alternate lines, ten times each:

ITM © Port, Kazman24 for loop usage (cont.) for ($i = 1; $i <= 20; $i++) { echo "\nMy robot loves me"; if ($i%2 == 0) echo " not"; } An alternate approach. Which is more "elegant"? Do Lab Exercise #3

ITM © Port, Kazman25 Practical Considerations When Using Loops The most common loop errors are unintended infinite loops and off-by-one errors in counting loops An off-by-one error is an error that occurs when a loop is executed one too many or one too few times. Sooner or later everyone writes an unintentional infinite loop To get out of an unintended infinite loop enter ^C (control-C) or stop button on browser Loops should tested thoroughly, especially at the boundaries of the loop test, to check for off-by-one and other possible errors

ITM © Port, Kazman26 Tracing a Variable in a Loop Tracing a variable: print out the variable each time through the loop A common technique to test loop counters and troubleshoot off-by-one and other loop errors Some systems provide a built-in tracing system that allows you to trace a variable without having to change your program. If no built-in utility is available, insert temporary output statements to print values.