Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Outline 5.1 REPETITION IN PROGRAMS
Repetition and while loops. Assignments Due – Lab 4.
CS201 - Repetition loops.
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 Repetition and Loop Statements Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
1 CS 201 Repetition Debzani Deb. 2 Overview Loops  Endfile-Controlled loop  Nested loop  Do-While loop  Flag-Controlled loop Hand Tracing the code.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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 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.
Lecture 12: Control Flow. Repetition and Loop structures COS120 Software Development Using C++ AUBG, COS dept.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CISC105 – General Computer Science Class 4 – 06/14/2006.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5: Repetition and Loop Statements
Repetition and Loop Statements
Chapter 6: Repetition Statements
Repetition and Loop Statements
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Unit 4 Repetition and Loops

Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for statement Sentinel-controlled loops Endfile-controlled loops Nested loops do-while statement Flag-controlled loops Debuggers Diagnostic statements Common errors

Figure 5.1 Flow Diagram of Loop Choice Process

Types of Loops Table 5.1

Counter-Controlled Loops Used when you know the number of times the loop must execute General steps: 1.Set loop control variable to 0 2.While loop control variable < final value Execute statements Increase loop control variable by 1

Figure 5.3 Flowchart for a while Loop

Figure 5.2 Program Fragment with a Loop

Figure 5.4 Program to Compute Company Payroll

Figure 5. Program to Compute Company Payroll (cont’d)

Compound Assignment Operators Table 5.3

Figure 5.5 Using a for Statement in a Counting Loop

for Loop Style Separate lines for each expression for (count_emp = 0; count_emp < number_emp; count_emp += 1 ) {…} All expressions on one line for (i = 0; i < max; i += 1) {…}

Figure 5.6 Comparison of Prefix and Postfix Increments

Figure 5.7 Function to Compute Factorial

Figure 5.8 Displaying a Celsius- to- Fahrenheit Conversion Table

Conditional Loops Execute until a condition is no longer true or becomes true. Example using a while statement: printf("Enter number of values> "); scanf("%d", &num_obs); while (num_obs < 0) { printf("Negative, try again> "); scanf("%d", &num_obs); }

Figure 5.9 Program to Monitor Gasoline Storage Tank

Figure 5.9 Program to Monitor Gasoline Storage Tank (cont’d)

Sentinel-Controlled Loop Data value is used to determine end- point. General format 1.Get a line of data 2.While the sentinel value has not been encountered a.Process the data line. b.Get another line of data.

Correct vs. Incorrect Sentinel Loop Correct 1.Initialize sum to 0 2.Get first score 3.while score is not the sentinel a.Add score to sum b.Get next score Incorrect 1.Initialize sum to 0 2.while score is not the sentinel a.Get score b.Add score to sum

Figure 5.10 Sentinel-Controlled while Loop

Sentinel Loop with a for Statement /* Accumulate sum of all scores. printf(“Enter first score (or %d to quit)> “, SENTINEL); for(scanf(“%d”, &score); score != SENTNEL; scanf(“%d”, &score)) { sum += score; printf(“Enter next score (%d to quit)> ”, SENTINEL);

Endfile-Controlled Loops Used to read a file into memory. Loop until EOF. General format: 1.Get the first data value and save input status. 2.While input status does not indicate that end of file has been reached. a.Process data value. b.Get next data value and save input status.

Figure 5.11 Batch Version of Sum of Exam Scores Program

Figure 5.12 Program to Process Bald Eagle Sightings for a Year

Figure 5.12 Program to Process Bald Eagle Sightings for a Year (cont’d)

Figure 5.13 Nested Counting Loop Program

do-while Statement Example 5.9 do { printf(“Enter a letter from A through E> “); scanf(“%c”, &letter_choice); } while (letter_choice ‘E’);

Flag-Controlled Loops Declare a flag variable of type int. Initialize the flag to a value that is not the exit condition. Loop on the flag becoming true or false. Set the flag within the loop to indicate exit condition.

Figure 5.14 Validating Input Using do-while Statement

Figure 5.15 Structure Chart for Computing Solar Collecting Area Size

Figure 5.16 Program to Approximate Solar Collecting Area Size

Figure 5.16 Program to Approximate Solar Collecting Area Size (cont’d)

Debugger Debugger allows: – Single-step execution – Setting breakpoints on a statement – Variable inspections

Diagnostic Calls Use printf to output intermediate results. Define a constant named DEBUG and use a conditional: #define DEBUG 1 if (DEBUG) printf("*** score is %d, sum is %d\n", score, sum);

Off by One Errors This loop executes n+1 times: for (count = 0; count <= n; ++count) sum += count; Always test at the loop boundaries to verify the loop does the right thing.

Common Errors Forgetting to use curly braces around multiple steps Termination condition never met (infinite loop) Mistyping an equality operator (==) as an assignment operator (=) Confusing do-while and while Mistakes related to operator precedence – Avoid increment, decrement, and compound assignment operators in complex expressions. – Use parentheses to control evaluation order.