PGT 106 - C Programming1 Week 4 – Repetition Structures / Loops.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

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.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter 5: Control Structures II (Repetition)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
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.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Control Structures Lecture 7.
- Additional C Statements
Outline Altering flow of control Boolean expressions
Chapter 4 - Program Control
Chapter 4 - Program Control
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

PGT C Programming1 Week 4 – Repetition Structures / Loops

PGT C Programming2 Outline Introduction While loops Three types of while loops Do-while loops For loops Nested loops

PGT C Programming3 Why Need Loops ? Suppose we want to add five numbers and find the average. From what you have learned so far, you could proceed as follows scanf(“%d %d %d %d %d”, &iNum1, &iNum2, &iNum3, &iNum4, &iNum5); iSum = iNum1 + iNum2 + iNum3 + iNum4 + iNum5; fAverage = iSum / 5; If 100 numbers, 1000 numbers?

PGT C Programming4 Repetition (Loop) Used to control the flow of a program Loops are basically repetitions or iterations used to repeat a segment of code Three statements can be used to implement loops in C while statement do-while statement for statement while and for statements are similar in implementation, but have different syntax The do-while statement is different - the following code is executed at least once

PGT C Programming5 The while loop structure The general form of the while statement is: while (expression) statement; To avoid an infinite loop, make sure that the loop’s body contains statement (s) that assure that the exit condition i.e. the expression in the while statement will eventually be false. while loop repeates until condition becomes false

PGT C Programming6 Flowchart of a while statement

PGT C Programming7 The while loop structure-cont There are basically three types of while loops: Counter-controlled while loop Sentinel-controlled while loop Flag-controlled while loop

PGT C Programming8 Counter-Controlled while Loops Definite repetition: know how many times loop will execute Control variable used to count repetitions Example: int iCounter = 1; // declare and initialize while ( iCounter <= 10 ) // test condition { printf( "%d\n", iCounter ); ++iCounter; // update } Requirement: 1. Declare and initialize control variable value (or loop counter) 2. A condition that tests for the final value of the control variable (i.e., whether looping should continue) 3. Update control variable (incr/decr)

PGT C Programming9 Counter-Controlled while Loops Another example: int iProduct = 2; while ( iProduct <= 1000 ) iProduct = 2 * iProduct; iProduct <= 1000 iProduct = 2 * iProduct true false declare and initialize test condition increment

PGT C Programming10 Sentinel-Controlled while Loops Indefinite repetition Used when number of repetitions not known and loop needs to input value repeatedly for each iteration Sentinel value indicates "end of data“

PGT C Programming11 Sentinel-Controlled while Loops-continued Example: int iNumber, iCount, iSum; iSum = 0; iCount = 0; printf(“To stop enter Enter positive numbers : " ); scanf(“%d”, &iNumber); while (iNumber != -999) { iSum = iSum + iNumber; //find sum of numbers entered iCount++; //count how many numbers entered printf(“To stop enter Enter positive numbers : " ); scanf(“%d”, &iNumber); } printf(“\nThe sum of %d numbers is %d“, iCount, iSum); Requirement: 1. Read control variable value before enter loop 2. A condition that tests control variable’s validity (i.e., whether looping should continue) 3. Read again control variable before end of loop Sentinel value

PGT C Programming12 Sentinel-Controlled while Loops-continued Another example: float fGradePt; char cChoice; printf(“Continue? y-yes n-no: ”); scanf(“%c”, &cChoice); while ( cChoice == ‘y’) {printf(“\nEnter grade point:”); scanf(“%f”, &fGradePt); if(fGradePt > 2.0) printf(“\nPass); printf(“Continue? y-yes n-no: ”); scanf(“%c”, &cChoice); } read control variable test condition read again

PGT C Programming13 Flag-Controlled while Loops Uses a flag to control the loop. Loop exit when expression is evaluated to false. int error; int iGuess; while (error) { printf("Enter number between 1 and 200:"); scanf("%d", &iGuess); if ((iGuess>= 88) &&(iGuess <=128)) {error = 0; printf("\nBINGO!");} } Requirement: 1. Set control variable before loop 2. A condition that tests control variable. If expr evaluated to 1, loop continue 3. A decision structure to test value validity 4. Set control variable to 0 to indicate found Set Flag, by default error =1 test condition decision structure Set error to 0

PGT C Programming14 The do - while Repetition Structure Similar to the while structure Condition for repetition is tested after the body of the loop is performed All actions are performed at least once Expression can be written as count-controlled or sentinel-controlled Format: do { statement; } while ( condition );

PGT C Programming15 Flowchart of a do-while structure true false action(s) condition

PGT C Programming16 The do - while Repetition Structure Example : prints the integers from 1 to 10 int iCounter = 1; do { printf( "%d ", iCounter ); } while (++iCounter <= 10);  Another example: do{ printf(“\nEnter grade point:”); scanf(“%f”, &fGradePt); if(fGradePt > 2.0) printf(“\nPass); printf(“Continue?y-yes n-no :”); scanf(“%c”, &cChoice); }while ( cChoice == ‘y’) count-controlled sentinel-controlled

PGT C Programming17 The do-while Repetition Structure To avoid an infinite loop, make sure that the loop body contains a statement that ultimately makes the expression false and assures that it exits Another example: int iLoop = 0; do { printf (“%d ”,iLoop); iLoop = iLoop + 5; } while (iLoop <= 20); The output is:

PGT C Programming18 The do-while Looping Structure (Example) Example: Consider the following two loops (a) iLoop = 11; while (iLoop <= 10) { printf("%d",iLoop); iLoop = iLoop + 5; } (b) iLoop = 11; do { printf("%d",iLoop); iLoop = iLoop + 5; } while (iLoop <= 10);

PGT C Programming19 The do-while Looping Structure (Example) In (a), the while loop, produces nothing In (b) the do...while loop, outputs the number 11

PGT C Programming20 The for Repetition Structure Format when using for loops for (initialization ; loop continuation test ; increment statement) Use for loops when already know how many times to repeat Example: for(iCounter = 1;iCounter <= 10;iCounter++) printf("%d\n",iCounter); Prints the integers from 1 to 10

PGT C Programming 21 The for Repetition Structure ‘for’ loops can usually be rewritten as while loops: initialization; while ( loop continuation test ) { statement; increment statement; } Initialization and increment Can be comma-separated lists. Example: int iVar1, iVar2; for(iVar1=0,iVar2=0;iVar2+iVar1<=10;iVar2++,iVar1++) printf(“%d\n”,iVar2+iVar1);

PGT C Programming22 Flow of a for statement

PGT C Programming23 Nested loop Loop within a loop Inner loop is performed first e.g. for(iLoop1=1;iLoop1<4;iLoop1++) { for(iLoop2=1;iLoop2<5;iLoop2++) printf(“%d”, iLoop2); printf(“\n”); } Output 1234

PGT C Programming24 Nested loop Another example: Program finds and prints avg of three test scores, then asks whether user wants to continue do { for(iCount=0; iCount <3;iCount++) {printf(“\nEnter test marks: ”); scanf(“%d”, &iMarks); iTotal=iTotal+iMarks; } fAvg=iTotal/iCount; printf(“\nAverage:%5.2f”, fAvg); printf(“\nContinue?”); scanf(“%c”, &cChoice); }while(cChoice == ‘y’);

PGT C Programming25 End Week 4 - Loops Q & A!