Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
© 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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Looping Constructs “Here we go loop de loop, on a Saturday night” – Johnny Thunder “First I'm up, and then I'm down. Then my heart goes around and around.”
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Iteration for Statements, while Statements and do-while Statements.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
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.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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,
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
ITM © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2016, Fred McClurg, All Rights.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Topic 4: Looping Statements
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.
Topics Introduction to Repetition Structures
ITM 352 Flow-Control: Loops
LOOPS.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Looping.
MSIS 655 Advanced Business Applications Programming
Repetition Control Structure
Chapter 5: Control Structures II (Repetition)
Topics Introduction to Repetition Structures
Presentation transcript:

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1

Chapter Four Gonna go ‘round in circles or Spinning wheel got to go round Ride a painted pony let the spinning wheel spin or I'm so dizzy, my head is spinnin' Like a whirlpool, it never ends 2 ourses/php/slides/chapter04.looping.ppt

Description: A looping construct which defines that while a condition is true, execute a block of code. Distinctives: 1. The condition is at the top of the loop. 2. The body executes zero or more times. 3. Often used to “protect” code from execution. Disadvantages : 1. Variables must be initialized prior to condition. 2. Condition must be updated within body of loop. 3. Caution must be used to avoid an infinite loop. The “while” Loop 3

block of code while condition FALSE TRUE Repeat Loop Exit Loop Continue Execution Begin Execution Preferred Usage: 1.When a condition must be met first before statement execution. The “while” Flow Chart Preferred Usage: 2.When the number of loop iterations is not known in advance.

Syntax: while ( condition ) // is true { statement;... } The “while” Syntax 5

<?php $min = 0; $max = 100; $magicNumber = rand( $min, $max ); // initialization before condition $guessCurrent = rand( $min, $max ); while ( $guessCurrent != $magicNumber ) { $guessWrong++; // prep for next iteration or exit $guessCurrent = rand( $min, $max ); } printf( "Mystery Number: %d Random Guesses: %d", $magicNumber, $guessWrong ); ?> “Pick a number between...” via while 6

Problem: Print out the numbers 1 to 10 in ascending order. Requirements: 1.Use a “while” loop to handle the iterations. 2.Use the “ ++ ” or “ += ” operator to increment the count. Output: Each number should be on a separate line. Student Exercise: Count up via “while” 7

<?php $count = 1; $max = 11; while ( $count < $max ) { printf( "%d ", $count ); $count++; } ?> Student Solution: Count up via “while” 8

Problem: Perform a computer simulation of the high/low guessing game. Requirements: 1.Use a “while” loop to handle the iterations. 2.Use “rand()” function to initialize target number and simulate guesses. 3.Use an “if / else” statement to narrow the range. 4.Count the number of wrong guesses. Input: A variable initialized to a random “mystery” number. Outputs: 1.The mystery number. 2.The number of guesses attempted. Student Exercise (High/Low Game) 9

<?php $min = 0; $max = 100; $wrong = 0; // init for ++ $mystery = rand( $min, $max ); // pick a "secret" number $guess = rand( $min, $max ); // try to guess secret number while ( $guess != $mystery ) { $wrong++; if ( $guess < $mystery ) // guess is too small $min = $guess + 1; // raise bottom range else // if ( $guess >= $mystery ) // guess is too large $max = $guess; // lower top range $guess = rand( $min, $max ); // narrow range & prep condition } printf( "Mystery Number: %d High/Low Guesses: %d", $mystery, $wrong ); ?> Student Solution (High/Low Game) 10

Description: A looping construct that executes a block of code while a condition is true. Also known as “repeat until” because loop continues until condition is false. Distinctives: 1.The condition is located at the bottom of the loop. 2.The body executes at least one or more times. Advantage: The expression that changes the variable used in the conditional statement of the loop, can be initialized and updated in a single location inside the body of the loop. Disadvantages : 1.Condition must be updated within body of loop. 2.Caution must be used to avoid an infinite loop. The “do... while” Loop 11

Preferred Usage: 1.When required to execute the loop at least once. 2.When not knowing the number of loop iterations. The “do... while” flow chart code block FALSE TRUE do while condition Repeat Loop Exit Loop Continue Execution Begin Execution

Syntax: do { statement;... } while ( condition ); // is true Note: The semi-colon at the end of the “do... while” statement is required. Pitfall: Omission of the semi-colon at the end of a statement is a common error. The “do... while” Syntax 13

<?php $yahtzee = 5; // matching dice $diceMin = 1; // smallest dice $diceMax = 6; // largest dice $correct = 0; // guesses matched $failed = 0; // wrong guesses // select initial value $choice = rand( $diceMin, $diceMax ); $correct++; // first roll do // while() { $current = rand( $diceMin, $diceMax ); // roll the dice if ( $current == $choice ) // dice matches first roll $correct++; else // dice doesn’t match $failed++; } while ( $correct < $yahtzee ); // do...while requires semi-colon! printf( "Yahtzee required %d rolls.", $failed ); ?> Yahtzee Simulation via “do... while” 14

Problem: Print out the numbers 10 to 1 in descending order. Requirements: 1.Use a “do...while” loop to handle the iterations. 2.Use the “ -- ” or “ -= ” operator to increment the count. Output: Each number should be on a separate line. Student Exercise: Count via “do...while” 15

<?php $count = 10; $min = 0; do // while { printf( "%d ", $count ); $count--; } while ( $count > $min ); ?> Student Solution: Count via “do...while” 16

Description: General purpose looping construct that uses a counter. Preferred Usage: 1.When the number of iterations is known. 2.When an index value (counter) is needed. The “for” Loop 17

Syntax: for ( initialization; condition; expression ) { statement;... } The “for” Syntax 18

Consider the following: <?php $init = 0; $max = 2; for ( $i = $init; $i < $max; $i++ ) printf( "%d ", $i ); ?> Code Trace (sequential steps): 1.$i = 0; // initialization happens once 2.0 < 2; // condition is true 3.printf( 0 ); // loop body executes 4.$i = 0 + 1; // increment 5.1 < 2; // condition is true 6.printf( 1 ); // loop body executes 7.$i = 1 + 1; // increment 8.2 < 2; // condition is false 9. // loop exits A “for” Example 19

Problem: Count from 0 to 50 in multiples of 5. Print out the numbers ascending order. Requirements: 1.Use a “for” loop to handle the iterations. 2.Use the “ ++ ” or “ += ” operator to increment the count. Output: Each number should be on a separate line. Student Exercise: Count via “for” 20

<?php $min = 0; $max = 50; $multiple = 5; for ( $count = $min; $count <= $max; $count++ ) { if ( ( $count % 5 ) == 0 ) printf( "%d ", $count ); } ?> Student Solution: “for” with an if 21

<?php $min = 0; $max = 50; $multiple = 5; for ( $count = $min; $count <= $max; $count += $multiple ) { printf( "%d ", $count ); } ?> Student Solution: “for” with an index 22

<?php for ( $i = 0, $j = 9; // multi init $i < 10; // condition $i++, $j-- ) // multi inc/dec { printf( "\$i: %d", $i ); echo str_repeat( " ", 5 ); printf( "\$j: %d", $j ); printf( " \n" ); } ?> A “for” with multiple indexes 23

Description: A “break” statement terminates execution of a loop. Example: <?php for ( $cnt = 0; $cnt < 10; $cnt++ ) { if ( $cnt == 4 ) break; // bail out of loop printf( "Count: %d ", $cnt ); } ?> Using “break” in a loop 24

Description: A “break” statement terminates the nearest enclosing loop. <?php for ( $x = -2; $x < 3; $x++ ) // outer loop { for ( $y = -2; $y < 3; $y++ ) // inner loop { if ( $y == 0 ) { // can't divide by zero break; // escape inner loop } $result = $x / $y; printf( "%d / %d = %f ", $x, $y, $result ); } // inner loop } // outer loop ?> Using “break” in multiple loops 25

Description: A “break” statement terminates the nearest enclosing loop. <?php for ( $x = -2; $x < 3; $x++ ) // outer loop for ( $x = -2; $x < 3; $x++ ) // outer loop { printf( "\$x=%d\t", $x ); printf( "\$x=%d\t", $x ); for ( $y = -2; $y < 3; $y++ ) for ( $y = -2; $y < 3; $y++ ) { if ( $y == 0 ) if ( $y == 0 ) break; // escape inner break; // escape inner printf( "\$y=%d\t", $y ); printf( "\$y=%d\t", $y ); } // inner loop } // inner loop printf( " " ); printf( " " ); } // outer loop } // outer loop ?> Tracing variables in loops containing “break” $x$y$y Inner Loop Outer Loop

Description: The “continue” statement stops processing the current loop iteration and jumps to the next iteration of the loop. Example: <?php for ( $cnt = 0; $cnt < 10; $cnt++ ) { if ( $cnt == 4 ) continue; // skip iteration printf( "Count: %d ", $cnt ); } ?> Using “continue” in a loop 27

Description: The “continue” statement skips processing the current iteration of the nearest enclosing loop and jumps to the next iteration of the loop. <?php for ( $x = -2; $x < 2; $x++ ) // outer loop { for ( $y = -2; $y < 2; $y++ ) // inner loop { if ( $y == 0 ) // can't divide by zero continue; // next inner $result = $x / $y; printf( "%d / %d = %f ", $x, $y, $result ); } // inner loop } // outer loop ?> Using “continue” in multiple loops 28

$x$y$y$y$y Description: The “continue” statement stops processing the current loop iteration. <?php for ( $x = -2; $x < 3; $x++ ) // outer loop for ( $x = -2; $x < 3; $x++ ) // outer loop { printf( "\$x=%d\t", $x ); printf( "\$x=%d\t", $x ); for ( $y = -2; $y < 3; $y++ ) // inner loop for ( $y = -2; $y < 3; $y++ ) // inner loop { if ( $y == 0 ) if ( $y == 0 ) continue; // skip one continue; // skip one printf( "\$y=%d\t", $y ); printf( "\$y=%d\t", $y ); } // inner loop } // inner loop printf( " " ); } // outer loop ?> Tracing variables in loops containing “continue” Inner Loop Outer Loop

to be continued... urses/php/slides/chapter05a.functions.ppt