Chapter 5: Repetition and Loop Statements By: Suraya Alias.

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
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.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
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.
CS 201 Selection Structures (2) and Repetition
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 5 (Loop Statements)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
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 4: Control Structures II
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.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
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 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.
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.
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.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
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.
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.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
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 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
REPETITION STATEMENTS
Chapter 5: Repetition and Loop Statements
Repetition and Loop Statements
Lec 6.
Chapter 6: Repetition Statements
Repetition and Loop Statements
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Chapter 5: Repetition and Loop Statements By: Suraya Alias

5.1 Repetitions in Program Loop –a control structure that repeats a group of steps in a program 3 loop control statements  While, for, do-while Loop body contains the statements to be repeated

Figure 5.1 Flow Diagram of Loop Choice Process 1-3

1-4 Loop types 1) Counting loopCan be determines before loop execution, exactly how many loop repetition will be needed to solve the problem While for 2) sentinel- controlled loop Input of a list of data of any length ended by a special value While, for 3) endfile- controlled loop Input of a single list of data of any length from a a data file While, for 4) Input validation loop Repeated interactive input of data value until a a value within the valid range is entered do-while 5) General conditional loop Repeated processing of data until a desired condition is met While, for

5.2 Counting loops and the while statement Counter-controlled loop (counting loop) is a loop whose required number of iterations can be determined before loop execution begins Format Set loop control variable = 0 While loop control variable < final value Increase loop control variable by 1

The While Statement while (count_emp < 7)  (count_emp < 7) is the loop repetition condition, a condition which control the loop repetition  The loop is repeated when the condition is true, the loop exited when false  count_emp is the loop control variable because it determines whether the loop body is repeated.  The loop control variable must be; Initialized, count_emp = 0 Tested, count_emp is tested before the start of each loop repetition Updated, count_emp is updated (value increased by 1) during each iteration

Syntax of while statement Syntax : while (loop repetition condition) Statement Example - /* display N star*/ count_star = 0; while (count_star < N) { printf(“*”); count_star = count_star + 1; }

Figure 5.2 Program Fragment with a Loop 1-8

Figure 5.3 Flowchart for a while Loop 1-9

5.3 Computing a Sum or a product in a Loop Accumulator – a variable used to store a value being computed in increments during the execution of a loop total_pay = total_pay + pay; /* add next pay*/ The value of total_pay increases with each loop iteration. To write general loops, the loop repetition condition can be manipulated  count_emp < number_emp  The number of employees must be scanned into variable number_emp before the while statement executes.  We also can use loop to compute the product of a list product = product * item;

Figure 5.4 Program to Compute Company Payroll 1- 11

Figure 5.4 Program to Compute Company Payroll (cont’d) 1- 12

Compound Assignment Operators Statement with simple assignment operator Equivalent statement with compound assignment operator count_emp = count_emp + 1;Count_emp +=1; time = time – 1;time -= 1; total_time = total_time + timetotal_time += time product = product * item;product *= item; n = n * (x + 1);n *= (x + 1);

5.4 The For Statement A for statement has 3 loop control component that is initialization, testing and update in one place. for (count_emp = 0; /*initialization*/ count_emp < number_emp; /*loop repetition condition*/ Count_emp +=1) /*update*/ Syntax : for (initialization expression; Loop repetition condition; Update expression) statement

Figure 5.5 Using a for Statement in a Counting Loop 1- 15

Increment and decrement operators for (counter = 0; counter < limit; ++counter) The side effect of applying the ++ operator is that the value of its operand is incremented by one. The value of the expression in which the ++ is used depends on the position of the operator  Prefix increment : j = ++i The value of the expression is the variables value after incrementing  Postfix increment : j = i++ The value of the expression is the variables value before incrementing

Figure 5.6 Comparison of Prefix and Postfix Increments 1- 17

Figure 5.7 Function to Compute Factorial 1- 18

Figure 5.8 Displaying a Celsius-to-Fahrenheit Conversion Table 1- 19

5.5 Conditional Loops Is used to control the number of time the loop executes/repeats. Such as continue prompting the user for data as long as the response is unreasonable. The loop can be process in a function subprogram, it simplifies the main function.

Figure 5.9 Program to Monitor Gasoline Storage Tank 1- 21

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

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

5.6 Loop design Sentinel-Controlled Loops  Sentinel value is an end marker that follows the last item in a list of data  Sentinel loop 1. Initialize sum to zero 2. Get first score 3. While score is not the sentinel 1. Add score to sum 2. Get next score

Figure 5.10 Sentinel-Controlled while Loop 1- 25

Sentinel Loop using For printf(“enter first score(or %d to quit) >”, sentinel); for (scanf(“%d”, &score); score !=sentinel; scanf(“%d”, &score)) { sum += score; printf(“Enter next score (%d to quit)>”,sentinel); }

Endfile-Controlled Loop & Infinite loops on faulty data Figure 5.11 Batch Version of Sum of Exam Scores Program Figure 5.11 Batch Version of Sum of Exam Scores Program 1- 27

5.7 Nested loops Figure 5.12 Program to Process Bald Eagle Sightings for a Year Figure 5.12 Program to Process Bald Eagle Sightings for a Year 1- 28

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

5.8 the do-while statement and Flag-Controlled Loops Syntax : do Statement while (loop repetition condition); Example : Do Status = scanf(“%d”,num); While (status > 0 && (num % 2) !=0); Flag = is a type int variable used to represent whether or not a certain event has occurred. The flag has the value of true(1) and false(0);

Figure 5.13 Nested Counting Loop Program 1- 31

Figure 5.14 Validating Input Using do-while Statement 1- 32

Figure 5.15 Structure Chart for Computing Solar Collecting Area Size 1- 33

Figure 5.16 Program to Approximate Solar Collecting Area Size 1- 34

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

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

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

5.10 How to debug and test Programs Using debugger programs  Using Single step execution or breakpoints Debugging without a debugger  Using printf or diagnostic call Loop boundaries = initial and final values of the loop control variable Test by running program using multiple test data

New C construct Counting for loop For (num=0; num < 26; ++num) { Square = num * num; Printf(“%5d %5d\n”, num, square); } Counting for loop with negative steps For (volts=20; volts>=-20; volts -=10) { current = volts/resistance; printf(“%5d %8.3f\n”, volts, current); } Sentinel controlled while loop product = 1; printf(“Enter %d to quit\n”, senval); printf(“Enter first number”); scanf(“%d, &dat”); while (dat != senval) { product *=dat; printf(“next number”); scanf(“%d”, &dat);} Endfile controlled while loop sum=0; status = fscanf(infil, “%d”, &n); while (status ==1){ sum += n; status = fscanf(infil, “%d”, &n); }

New C construct do-while loop do { Printf(“positive number < 10”); sacnf(“%d”, &num); } while (num = 10); Increment/ decrement Z= ++j * k--; Compound Assignment Ans *= a – b;