Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
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.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
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.
 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)
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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)
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.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
© 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.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
 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:
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
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.
Outline Altering flow of control Boolean expressions
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops

Sesi 0607EKT120/4 Computer Programming Outline Introduction While loops  Three types of while loops Counter-controlled (definite repetition) Sentinel-controlled (indefinite repetition) Flag-controlled (uses Boolean variable) Do-while loops For loops Nested loops Math.h

Sesi 0607EKT120/4 Computer Programming Review  Problem that can be solve with computers generally consist of three: Computational – problem with mathematical processing Logical – problem involving with relational or logical processing. This is kind of processing involve in decision making. Repetitive – problem involving repeating a set of mathematical or logical instructions.

Sesi 0607EKT120/4 Computer Programming Suppose we want to add five numbers and find the average. scanf(“%d %d %d %d %d”, &num1, &num2, &num3, &num4, &num5); Why Need Loops? sum = num1 + num2 + num3 + num4 + num5; average = sum / 5; But what if…………………………. 100 numbers, 1000 numbers?

Sesi 0607EKT120/4 Computer Programming Repetition (Loop) We used to control the flow of a program. Loops are basically used to repeat a segment instructions. (Refer diagram Instruction 1 & 2 being repeated until the condition is false) Condition usually is a counter, flag status or a sentinel value.  Counter is a definite number of how many loops we need to execute the segment of code  Flag status is a logic operator status  Sentinel usually use if the number of loops to be execute is indefinite.

Sesi 0607EKT120/4 Computer Programming Repetition (Loop) In C language there are three types of loops that we can use.  while loop  do-while loop, this is equivalent with repeat … until loop in Pascal and other programming language.  for loop

Sesi 0607EKT120/4 Computer Programming The while loop structure The general syntax of while loop statement is: while (condition) statement;  If we have more than one statements, While (condition) { statement1; statement2; }

Sesi 0607EKT120/4 Computer Programming The while loop – continue To avoid an infinite loop, make sure that the loop’s body contains statement(s) that assure the exit condition i.e. the expression in the while statement will eventually be false. While loop repeated until condition becomes false

Sesi 0607EKT120/4 Computer Programming The while loop There are basically three types of while loops:  Counter-controlled while loop  Sentinel-controlled while loop  Flag-controlled while loop

Sesi 0607EKT120/4 Computer Programming Counter-Controlled while Loops Definite repetition: know how many times loop will execute. if we know how many loops to execute this is a counter control loop. Example Let consider this problem – To display number 1 to 10 on screen Refer flowchart for the solution

Sesi 0607EKT120/4 Computer Programming Counter control while loop Below is the example of C code for the solution Counter control variable used to count repetitions Example: int counter = 1; // declare and initialize while ( counter <= 10 ) // condition { printf( "%d\n", counter ); ++counter; // increase counter } 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)

Sesi 0607EKT120/4 Computer Programming Sentinel-Controlled while Loops Indefinite repetition – we not sure how many loops will be executed. Used when number of repetitions not known and the termination of the loop needs to be input value repeatedly for each iteration Sentinel value indicates "end of data“

Sesi 0607EKT120/4 Computer Programming Sentinel-Controlled while Loops Example: int main () { int number, count, sum; printf(“To stop enter Enter positive numbers : " ); scanf(“%d”, &number); while (number != -999) { sum = sum + number; // find sum of numbers entered count++; // count how many numbers entered printf(“To stop enter Enter positive numbers : " ); scanf(“%d”, &number); } printf(“\nThe sum of %d numbers is %d“, count, sum); } 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

Sesi 0607EKT120/4 Computer Programming Sentinel-Controlled while Loops-cont Another example: int main (){ char choice=‘y’; float grade_pt; while ( choice == ‘y’) {printf(“\nEnter grade point:”); scanf(“%f”, &grade_pt); if(grade_pt > 2.0) printf(“\nPass); printf(“Continue?y-yes n-no :”); scanf(“%c”, &choice); } read control variable test condition

Sesi 0607EKT120/4 Computer Programming Flag-Controlled while Loops Uses a boolean variable to control the loop. Loop exit when expression is evaluated to false. Requirement: 1.Set control variable to false before loop 2. A condition that tests control variable. If expression evaluated to true, loop continue 3. A decision structure to test value validity 4. Set control variable to true to indicate found bool found = false; while (!found) { printf(“Enter number between 1 and 200:”); scanf(%d”, &guess); if ((guess>= 88) &&(guess <=128)) {found = true; printf(“\nBINGO!”); } Set to false test condition Decision structure Set to true

Sesi 0607EKT120/4 Computer Programming The do - while Repetition Structure Similar to the while structure Condition for repetition 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 );

Sesi 0607EKT120/4 Computer Programming The do - while Repetition Structure Counter control example : prints the integers from 1 to 10 int counter = 1; do { printf( "%d ", counter ); } while (++counter <= 10);  Sentinel control example: do{ printf(“\nEnter grade point:”); scanf(“%f”, &grade_pt); if(grade_pt > 2.0) printf(“\nPass); printf(“Continue?y-yes n-no :”); scanf(“%c”, &choice); }while ( choice == ‘y’) count-controlled sentinel-controlled

Sesi 0607EKT120/4 Computer Programming 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 i = 0; do { printf(“%d”,i); i = i + 5; } while (i <= 20); The output is:

Sesi 0607EKT120/4 Computer Programming The do-while Looping Structure Example Consider the following two loops i = 11; while (i<=10) { printf(“%d”,i); i = i + 5; } i = 11; do {  printf(“%d”,i);  i = i + 5;  } while (i<=10); Is it same???? (a) (b)

Sesi 0607EKT120/4 Computer Programming The do-while Looping Structure - Example In (a), the while loop, produces nothing In (b) the do...while loop, outputs the number 11 Comment : The do while loop will permit at least once to enter the loop. Even though the condition is false. This is because the test condition is at the end of the loop

Sesi 0607EKT120/4 Computer Programming The for loop structure The for loop format  for ( counter init; end condition; increment statement) We use for loops structure when we already know how many times to repeat Example: for (counter = 1; counter <= 10;counter++ ) printf( "%d\n", counter );

Sesi 0607EKT120/4 Computer Programming The for Repetition Structure Initialization and increment  Can be comma-separated lists  Example 1: for (i=0; i<100; i++) printf(“%d\n”,i);  Example 2: for (int i = 0, j = 0; j + i <= 10; j++, i++) printf( "%d\n", j + i );

Sesi 0607EKT120/4 Computer Programming Flow of a for statement For loops can usually be rewritten as while loops: initialization; while ( loopContinuationTest ) { statement; increment statement; }

Sesi 0607EKT120/4 Computer Programming Nested loop When loop within a loop we called it nested loop (same as nested if(if within if statement)) Inner loop will be performed first E.g. for(i=1;i<4;i++){ for(j=1;j<5;j++) printf(“%d ”, j); printf(“\n”);} Usually we use in matrix operations. Output

Sesi 0607EKT120/4 Computer Programming Nested loop Another example:  Program find and print avg of three test scores, then asks whether want to continue do{ for(count=0; count <3;count++) {printf(“\nEnter test marks:”); scanf(“%d”, marks); total=total+marks; } avg=total/count; printf(“\nAverage:%5.2f”, avg); printf(“\nContinue?”); scanf(“%c”, &choice); }while(choice == ‘y’);

Sesi 0607EKT120/4 Computer Programming End Week 5 - Loops