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.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
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.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
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.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
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.
Computer Programming -1-
Chapter 6: Loops.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Programming Fundamentals Lecture #6 Program Control
For & do/while Loops.
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

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 after 10 iterations or terminate when the user types NO) Careful! If there is no terminating condition, then your program will never finish

2 Loops Pre-Test Loop check condition if false, exit the loop if true, execute statements, iterate: check condition if false, exit the loop if true, execute statements, iterate: etc. The block of statements may not be executed at all (if condition is immediately false) The condition must be updated is( condition )true? { block of statements; } yes no star t finish initialize condition

3 Loops Pre-Test Loop count-driven : uses a "counter" to determine how many times it iterates Example: every time the loop iterates, a counter variable is incremented by one. When the counter reaches a specific value, the condition becomes false and the loop terminates. event-driven : uses an "event" to determine how many times it iterates Example: at each iteration the user is asked whether to continue. As long as the user types "yes" the loop iterates. When the user types "no" the condition becomes false and the loop terminates.

4 for loops Pre-test, mainly count-driven Syntax: Example: for (init; condition; update) { statements; } /* Frog lifetime*/ int days; for (days =155; days > 0; days--) { work_all_day(); sleep_all_night(); } die_quietly();

5 while loops Pre-test, mainly event-driven Syntax: Example (event): while (condition) { statements; } /* Frog Feeding */ while ( am_hungry() == TRUE && see_fly() == TRUE ) { flick_tongue(); clamp_mouth(); swallow_fly(); }

6 while loops Pre-test, mainly event-driven Syntax: Example (counter): while (condition) { statements; } /* Frog lifetime*/ int days; days = 155; /* initialize */ while ( days > 0 ) { /* condition */ work_all_day(); sleep_all_night(); days--; /* update */ } die_quietly();

7 Loops Post-Test Loop execute statements check condition if false, exit the loop if true, iterate: execute statements check condition if false, exit the loop if true, iterate: etc. The block of statements is always executed at least once The condition must be updated is( condition )true? { block of statements; } yes no finish star t initialize condition

8 Loops Post-test loops The block of statements is ALWAYS executed at least once! Often used for data validation (e.g. if the user types a wrong selection, keep asking for a correct one) A post-test loop may be count- or event-driven.

9 do-while loop Post-test, mainly event driven Syntax: Example: do { statements; } while (condition) /* Frog mating*/ do { have_mate = look_for_lady_frog(); } while ( have_mate == FALSE )

10 Loops In some cases, we may need to break put of a loop prematurely. To do that, we use a break statement. Example: int days; float food,fat;... for( days = 155; days > 0; days--) { work_all_day(); if ( food+fat < 0.01) break; sleep_all_night(); } die_quietly();

11 Loops In some cases, we may want to only execute part of the body during an iteration. To do that, we use a continue statement. Example: /* Frog feeding v2.0 */ while ( am_hungry() == TRUE && see_fly() == TRUE ) { flick_tongue(); if (!caught_fly()) continue; clamp_mouth(); swallow_fly(); }

12 break vs. continue vs. return continue it is used in loops it means : skip the remaining statements in the loop body and iterate again break it is used in loops and switch statements it means : skip the remaining statements in this block, break out of the block. When used in loops, it causes the loop to terminate return it may be used anywhere in a function. it means : terminate the function.