A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Repetition There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is.
Repetition Control Structures
Repetition control structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 Lecture 15 Chapter 6 Looping Dale/Weems/Headington.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
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.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Chapter 6 Looping Dale/Weems. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
© 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.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
Chapter 6 Looping.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
Logic Structure - focus on looping Please use speaker notes for additional information!
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
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 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Looping. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to.
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
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.
Visual Basic Programming
Control Structures Repetition or Iteration or Looping Part II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CS 100 Introduction to Computing Seminar October 7, 2015.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Files. 5.1 The Increment and Decrement Operators.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
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.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
REPETITION CONTROL STRUCTURE
Chapter 6 Looping.
Programming Logic and Design Fourth Edition, Comprehensive
Computer Science Faculty
Control Structure Senior Lecturer
Loops A loop is a repetition control structure.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
REPETITION Why Repetition?
Presentation transcript:

A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading decision loop - control statement before body trailing decision loop - control statement after body Counted loop- for Logical loop - WHILE..ENDWHILE Loops

loops2 Leading Decision WHILE condition body of loop – group of one or more statements indent one level ENDWHILE

When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE TRUE body statement Expression

loops for While Loops 1.Initial condition 2.Test - WHILE(…) 3.Body 4.Next - often the same as step 1

loops5 Example Display "Do you want to play?(y/n)"1 Input ans WHILE (ans = ‘y’) OR (ans = ‘Y’)2 …//body3 Display “Do you want to continue(y/n)?”4 Input ans ENDWHILE Display “Thanks for playing!”

loops6 Loops Sentinel controlled keep processing data until a special value is entered to indicate that processing should stop Read blood pressures until a special value (like -1) selected by you is read. Count Controlled keep processing data for a specified number of times Read 100 blood pressures. End-of-file Controlled keep processing data as long as there is more data in the file Read all the blood pressures from a file no matter how many are there Flag Controlled keep processing data while a flag condition is true Read blood pressures until a dangerously high BP (200 or more) is read.

A Sentinel-controlled Loop Read numbers until -1, 999 Not always easy to determine sentinel value requires a “priming read” “priming read” means you read one set of data before the WHILE

loops Sentinel Value 1.Initial condition Get first value 2.Test - WHILE(…) While (val != sentinel) 3.Body 4.Next - often the same as step 1 Get next value

// Sentinel controlled loop total = 0; Display "Enter a blood pressure (-1 to stop ) "1 Input thisBP; WHILE (thisBP != -1) // WHILE not sentinel2 total = total + thisBP;3 Display “Enter a blood pressure (-1 to stop ) ”4 Input thisBP ENDWHILE Display total

loops10 Example Input number WHILE (number < 0) Display “Enter positive values only!” Input number ENDWHILE

loops11 Reading in records with sentinel value Trailer record Read fahrTemp1 WHILE fahrTemp <> cels_temp = (5 * (fahrTemp – 32))/9 3 Print fahrTemp, celsTemp Read fahrTemp4 ENDWHILE

End-of-File Controlled Loop depends on fact that a file goes into fail state when you try to read a data value beyond the end of the file No trailer record WHILE (there is a record) WHILE (not end of file) WHILE (records exist) Computer indicates there are no more records by sending a signal to the program Must read record before entering loop – there may be no records

loops Reading in Records 1.Initial condition Read first record 2.Test - WHILE(…) – While (not eof) 3.Body 4.Next - often the same as step 1 Read next record

loops14 Example Read fahrTemp1 WHILE (there is a record)2 cels_temp = (5 * (fahrTemp – 32))/93 Print fahrTemp, celsTemp Read fahrTemp4 ENDWHILE

Open file total = 0; Read thisBP; // priming read 1 WHILE (there is a record) 2 total = total + thisBP 3 Read thisBP // read another 4 END WHILE Display total; // End-of-file controlled loop

Do something a set number of times Need counter –initialize –increment iteration counter - incremented during each iteration of the loop event counter - incremented each time a particular event occurs Count-controlled loop

loops Count 1.Initial condition Initialize counter 2.Test - WHILE(…) While (counter > 0) 3.Body 4.Next – Increment Counter

//Print Hello 10 times Declare integer count count = 0;1 WHILE (count < 10)2 Display "Hello"3 count = count + 14 ENDWHILE Known Count

//Print Hello 10 times Declare integer count Display "How many times should we print Hello?" Input count1 WHILE (count > 0)2 Display "Hello"3 count = count -1 4 ENDWHILE Count is variable

loops20 Accumulators and Counters To find the average of a group of numbers-need running total and how many numbers Counter – storage area in which we count –increment –counter = counter + 1 –paychecks = paychecks + 1 Accumulator – storage area for keeping cumulative or running totals –accumulator = accumulator + number –total_wages_paid = total_wages_paid + net_pay –Total = total + num You must initialize the values of accumulators and counters

Declare integer thisBP, total, count Open file count = 0 // initialize 1 total = 0 Read thisBP WHILE ( count < 100 AND there is a record) 2 total = total + thisBP ; 3 count= count + 1 Read thisBP;4 END WHILE Display “The total = “ total Display "The average is " total/count 21

loops22 Infinite Loop index = 1 WHILE (index < 5) Print “Good Morning!” ENDWHILE

loops23 Never executed WHILE ans = “yes” …. Print “Do you want to add another number?” Input answer ENDWHILE

loops24 Don't forget to prime the loop! Initialize initial condition by reading in or setting value Input ans WHILE (ans = 'y') index = 0 WHILE (index < 10) Read name, ssNum,phone WHILE (there is a record)

loops25 Declare integer count Declare real total, avg, num total = 0 count = 0 Input num WHILE not end-of-file total = total + num count = count + 1 Input num ENDWHILE IF count = 0 THEN Print “No numbers entered” ELSE avg = total/count Print “The average is “,avg ENDIF

loops26 Trailing Decision Loop REPEAT Body UNTIL condition Test at the bottom Statements are executed at least once

loops27 Trailing decision loop Body condition FALSE TRUE

loops28 Example REPEAT Display “Enter two numbers” Input num1,num2 Display num1, “ + “, num2, “ = “, num1+num2 Display “Do you want to enter two numbers again?” Input ans UNTIL ans = “no”

loops29 Counted loop Fixed number of iterations Use a variable as a counter which starts at a specified number and increments the variable each time the loop is processed Repeats until the counter is greater than an ending number Beginning value, ending value, increment value

loops30 Counted Loop DO counter = initial_value to end_value … ENDDO Automatic: 1.When the computer executes the DO instruction, it sets the counter equal to the initial value 2.When the loop executes the ENDDO, it increments the counter 3.When the counter is less than or equal to the ending number, the processing continues to the body. When the counter is greater than the ending number, the processing continues at the instruction that follows the ENDDO instruction.

loops31 Examples DO count = 1 to 10 Display "Hello" ENDDO DO count = 1 to 100 Display count ENDDO DO num = 10 to 0 step - 1 Display num ENDDO Display "Blast off"