CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes -----------------------------------------  Define the concept of repetition structure.  Specify.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Advertisements

Outline 5.1 REPETITION IN PROGRAMS
Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
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.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 5: Loops and Files.
An Introduction to Programming with C++ Fifth Edition
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
1 ICS103 Programming in C Ch5: Repetition and Loop Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 5 Repetition and Loop Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville,
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
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.
© 2012Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
CISC105 – General Computer Science Class 4 – 06/14/2006.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter 5 Repetition and Loop Statements. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.5-2 Figure 5.1 Flow Diagram of Loop Choice Process.
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.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 4 Repetition Structures
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
CHAPTER 6: REPETITION AND LOOP STATEMENTS
CHAPTER 5A Loop Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Repetition and Loop Statements
Repetition and Loop Statements
Chapter 6: Repetition Statements
Repetition and Loop Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify the repetition of a group of program statements using the while, for and do-while.  Apply counter-controlled repetition sentinel- controlled and flag – controlled in program.  Design loops using algorithm for solving problems. Reference... Hanly, Koffman, C Problem Solving and Program Design in C, Fifth Edition, Pearson International Edition. Prepared By: Pn. Nik Maria Nik Mahamood (Coordinator DDC 1012)

Continue.. Learning outcomes  Convert problem design into C code using repetition controls.  Trace an algorithm or program to verify that it does what user expected.  Write a program using the combination of repetition and selection statements that control the flow of program execution  Debug and test a program. Reference... Hanly, Koffman, C Program Design for Engineers, Second Edition, Addison Wesley, Prepared By: Pn. Nik Maria Nik Mahamood (Coordinator DDC 1012)

This chapter introduces to: Repetition in Programs Counting Loops and the while Statement Computing a Sum or a Product in a Loop The for Statement Conditional Loops Loop Design Nested Control Structures The do-while Statement and Flag controlled Loops

REPETITION IN PROGRAMS Loop body – the statements that are repeated in the loop Figure 6.1 Flow Diagram of Loop Choice Process Figure 6.1 Flow Diagram of Loop Choice Process source: This figure is taken from Pearson Addison-Wesley, 2004.

KindWhen usedC Implementation structures Counting loopCan determine before loop execution exactly how many loop repetitions will be needed to solve the problem while for Sentinel-controlled loopInput of a list of data of any length ended by a special value. While For Endfile-controlled loopInput of a single list of data of any length from a data file While For Input validation loopRepeated interactive input of data of any length from a data file. Do-while General validation loopRepeated processing of data until a desired condition is met While For Table 6.1 Comparison of Loop Kinds

Counter-controlled loop – a loop whose required number of iterations can be determined before loop execution begins. Syntax: initialization expression; while (loop repetition condition){ statement; update expression; } Example: int x = 0; while (x < 4) { printf(“The value of x is %d”, x); x = x + 1; } COUNTING LOOPS AND THE WHILE STATEMENT

Figure 6.2 Program Fragment with a Loop Figure 6.2 Program Fragment with a Loop source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 6.3 Flowchart for a while Loop Figure 6.3 Flowchart for a while Loop source: This figure is taken from Pearson Addison-Wesley, 2004.

The loop control variable must be –Initialized –Tested –Updated for the loop to execute properly Each step is summarized as: –Initialization : set to an initial value before the while statement is reached. –Testing: tested before the start of each loop repetition –Updating: updated during each iteration.

Loop often accumulate a sum or a product by repeating an addition or multiplication operation as demonstrated in example 1 and 2. Example 1 (program to compute company payroll): –See page 216 – 219 Example 2 (program to calculate product) –See page COMPUTING A SUM OR A PRODUCT IN A LOOP

Figure 6.4 Program to Compute Company Payroll Figure 6.4 Program to Compute Company Payroll Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 6.4 Program to Compute Company Payroll (cont’d) Figure 6.4 Program to Compute Company Payroll (cont’d) Source: This figure is taken from Pearson Addison-Wesley, 2004.

C provides the for statement as another form for implementing loops. Syntax: for(initialization expression; loop repetition condition; update expression){ statement; } Example: int x; for (x = 0; x < 4; x++) { printf(“The value of x is %d”, x); } THE FOR STATEMENT

Figure 6.5 Using a for Statement in a Counting loop Figure 6.5 Using a for Statement in a Counting loop Source: This figure is taken from Pearson Addison-Wesley, 2004.

Increment and Decrement Operators The counting loop that you have seen have all included assignment expression of the form Counter = Counter + 1; Counter = Counter - 1 or Counter += 1; Counter -= 1; The increment operator ++ means that the value of its operand in incremented by one. The decrement operator -- means that the value of its operand in decremented by one.

Figure 6.6 Comparison of Prefix and Postfix Increments Figure 6.6 Comparison of Prefix and Postfix Increments Source: This figure is taken from Pearson Addison-Wesley, 2004.

Increment and Decrement Other than 1 Example Counter = Counter + 5; Counter = Counter - 2 or Counter += 5; Counter -= 2; See example 5.4 at page 227: program to convert temperature from Celsius to Fahrenheit.

Figure 6.7 Display a Celsius to Fahrenheit Conversion Figure 6.7 Display a Celsius to Fahrenheit Conversion Source: This figure is taken from Pearson Addison-Wesley, 2004.

In many programming situations, we will not be able to determine the exact number of loop repetitions before loop execution begin. In this case, we still able to write a condition to control the loop by prompting the user to input a data value. See example page : Program to monitor gasoline storage tank. CONDITIONAL LOOPS

Figure 6.8 Program to Monitor Gasoline Storage Tank Figure 6.8 Program to Monitor Gasoline Storage Tank Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 6.8 Program to Monitor Gasoline Storage Tank (cont’d) Figure 6.8 Program to Monitor Gasoline Storage Tank (cont’d) Source: This figure is taken from Pearson Addison-Wesley, 2004.

Problem solving questions for loop design as following table (based on program to monitor gasoline storage tanks. LOOP DESIGN QuestionsAnswerImplication for the algorithm What are the inputs? Initial supply of gasoline (barrels) Amount removed (Gallons) Input variable needed start_supply remov_gal Value of start_supply must be input once, but amounts removed are entered many times.

QuestionsAnswerImplication for the algorithm What are the output? Amount removed in gallons and barrels, and the current supply of gasoline. Values of current and remov_gals are echoed in the output. Output variable needed: remov_brls Is there any repetition? Yes. One repeatedly 1. Get amount removed 2. Convert the amount to barrels 3. Subtract the amount removed from the current supply 4. Checks to see whether the supply has fallen below the minimum. Program variable needed min_supply

QuestionsAnswerImplication for the algorithm Do I know in advance how many times steps will be repeated? NoLoop will not be controlled by a counter How do I know how long to keep repeating the steps? As long as the current supply is not below the minimum. The loop condition is Current >= min min_supply

Sentinel-Controlled Loops The sentinel value – to signal the program to stop reading and processing new data. Sentinel value is an end marker that follows the last item in a list of data. See example 5.6 at page 241: A program that calculates the sum of collection of exam is a candidate for using a sentinel value.

Figure 6.9 Sentinel-Controlled while Loop Figure 6.9 Sentinel-Controlled while Loop Source: This figure is taken from Pearson Addison-Wesley, 2004.

Using a for statement to implement a Sentinel Loops The for statement form pf the while loop in figure 6.9 as follows: /* Accumulate sum of all scores printf(“Enter first score (or %d to quit)>”, SENTINEL); for(scanf(“%d”, &score); score != SENTINEL; scanf(“%d”, &score)) { sum += score; printf(“Enter next score (or %d to quit)>”, SENTINEL); }

Loops may be nested just like other control structures. Nested loops consist of an outer loop with one or more inner loops. See example 5.7 at page : Program to process bald eagle sightings for a year. NESTED LOOPS

Figure 6.10 Program to Process Bald Eagle Sightings for a Year Figure 6.10 Program to Process Bald Eagle Sightings for a Year Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 6.10 Program to Process Bald Eagle Sightings for a Year (cont’d) Figure 6.10 Program to Process Bald Eagle Sightings for a Year (cont’d) Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 6.11 Nested Counting Loop Program Figure 6.11 Nested Counting Loop Program Source: This figure is taken from Pearson Addison-Wesley, 2004.

Do-while statement evaluate a loop repetition condition at the end of the loop body. Syntax: initialization expression; do { statement; update expression; } while (loop repetition condition); Example: int x = 0; do { printf(“The value of x is %d”, x); x = x + 1; } while (x < 4); THE DO-WHILE STATEMENT AND FLAG- CONTROLLED LOOPS

Flag-Controlled Loops for input validation The loop repetition condition may be simplified by using a flag A flag is a type int variable to represent whether or not a certain events has occurred. A flag has one of two values: 1 (true) and 0 (false). See example 5.10 at page : Program validating input using do-while statement

Figure 6.12 Validating Input Using do-while Statement Figure 6.12 Validating Input Using do-while Statement Source: This figure is taken from Pearson Addison-Wesley, 2004.