ITM 352 Flow-Control: Loops

Slides:



Advertisements
Similar presentations
Creating PHP Pages Chapter 7 PHP Decisions Making.
Advertisements

Objectives Using functions to organize PHP code
Loops (Part 1) Computer Science Erwin High School Fall 2014.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
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.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
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.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120 Introduction to Computer Science Iteration (Looping)
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
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.
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
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Working with Loops, Conditional Statements, and Arrays.
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.
CSI 3125, Preliminaries, page 1 Control Statements.
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.
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.
ITM © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8.
JavaScript, Sixth Edition
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
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
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Control Structures.
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.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Arrays, For loop While loop Do while loop
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Objectives In this chapter, you will:
Web Programming– UFCFB Lecture 20
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Chapter 3 Flow of Control Loops in Java.
Looping and Repetition
Presentation transcript:

ITM 352 Flow-Control: Loops

Iteration Traditional method of repeating statements loop control { --------------- } repeat these statements zero or more times, controlled by loop control

Repeating Code A loop statement is a control structure that repeatedly executes a statement or a series of statements while a specific condition is TRUE or until a specific condition becomes TRUE There are four types of loop statements: while statements for statements do...while statements foreach statements don't use! will cover later!

while Statements Tests the condition prior to executing the series of statements at each iteration of the loop The syntax for the while statement is: while (conditional expression) { statement(s); } As long as the conditional expression evaluates to TRUE, the statement or command block that follows executes repeatedly

while Statements (continued) Each repetition of a looping statement is called an iteration A while statement keeps repeating until its conditional expression evaluates to FALSE A counter is a variable that increments or decrements with each iteration of a loop statement

while Statements (continued) $Count = 1; while ($Count <= 5) { echo " $Count<br /> "; ++$Count; } echo " <p>You have printed 5 numbers.</p> ";

while Statements (continued) $Count = 10; while ($Count > 0) { echo “$Count<br />”; --$Count; } echo " <p>We have liftoff. </p> ";

while Statements (continued) $Count = 1; while ($Count <= 100) { echo " $Count<br /> "; $Count *= 2; }

while Statements (continued) In an infinite loop, a loop statement never ends because its conditional expression is never FALSE $Count = 1; while ($Count <= 10) { echo " The number is $Count "; } PHP Programming with MySQL, 2nd Edition

for Statements Combine the initialize, conditional evaluation, and update portions of a loop into a single statement Repeat a statement or a series of statements as long as a given conditional expression evaluates to TRUE If the conditional expression evaluates to TRUE, the for statement executes and continues to execute repeatedly until the conditional expression evaluates to FALSE

for Statements Can also include code that initializes a counter and changes its value with each iteration The syntax of the for statement is: for (counter declaration and initialization; condition; update statement) { statement(s); }

for Statement Example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }

for Statements Example for ($Count = 0; $Count < 5; $Count++) { echo "This is the " . $Count ; switch ($Count) case '1': echo "st"; break; case '2': echo "nd"; case '3': echo "rd"; default: echo "th"; } echo " time through the loop <br /> ";

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

The exit Function for ($Count = 0; $Count < 5; $Count++) { echo "Iteration " . $Count . "<BR>"; if ($Count == 3) exit("I'm outta here!"); }

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

break example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 3) break; elseif ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }

continue example for ($potato = 1; $potato < 5; $potato++) { if ($potato == 3) continue; elseif ($potato == 4) echo $potato . "<BR>"; else echo $potato . " potato<BR>"; }

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

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.