More Loops Topics Counter-Controlled (Definite) Repetition

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

Computer Science 1620 Loops.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 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.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
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.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
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.
Lesson #5 Repetition and Loops.
Chapter 4 – C Program Control
Chapter 5: Structured Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Lesson #5 Repetition and Loops.
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Quick Test What do you mean by pre-test and post-test loops in C?
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Control Structures Lecture 7.
The while Looping Structure
- Additional C Statements
Lesson #5 Repetition and Loops.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Structured Program
Chapter 4 - Program Control
Loops in C.
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
Chapter 6: Repetition Statements
UMBC CMSC 104 – Section 01, Fall 2016
REPETITION STATEMENTS
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
Lesson #5 Repetition and Loops.
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
Loops.
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
The while Looping Structure
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing an Appropriate Loop Break and Continue Statements Reading Sections 5A.4 – 5A.6

Counter-Controlled Repetition (Definite Repetition) If it is known in advance exactly how many times a loop will execute, it is known as a counter-controlled loop. int i = 1 ; while ( i <= 10 ) { printf(“i = %d \n”, i) ; i = i + 1 ; }

Counter-Controlled Repetition (con’t) Is the following loop a counter-controlled loop? while ( x != y ) { printf( “ x = %d ”, x) ; x = x + 2 ; }

Event-Controlled Repetition (Indefinite Repetition) If it is NOT known in advance exactly how many times a loop will execute, it is known as an event-controlled loop. sum = 0 ; printf(“Enter an integer value: ” ) ; scanf(“ %d ”, &value) ; while ( value != -1) { sum = sum + value ; printf(“Enter another value: ” ) ; }

Event-Controlled Repetition (con’t) An event-controlled loop will terminate when some event occurs. The event may be the occurrence of a sentinel value, as in the previous example. There are other types of events that may occur, such as reaching the end of a data file.

The 3 Parts of a Loop #include <stdio.h> int main () { int i = 1 ; initialization of loop control variable /* count from 1 to 100 */ while ( i < 101 ) test condition that terminates loop printf (“%d ”, i) ; i = i + 1 ; modification of loop control } variable return 0 ; }

The for Loop Repetition Structure The for loop handles details of the counter-controlled loop “automatically”. The initialization of the the loop control variable, the termination condition test, and control variable modification are handled in the for loop structure. for ( i = 1; i < 101; i = i + 1) { initialization modification } test

When Does a for Loop Initialize, Test and Modify? Just as with a while loop, a for loop initializes the loop control variable before beginning the first loop iteration, modifies the loop control variable at the very end of each iteration of the loop, and performs the loop termination test before each iteration of the loop. The for loop is easier to write and read for counter-controlled loops.

A for Loop That Counts From 0 to 9 for ( i = 0 ; i < 10 ; i = i + 1 ) { printf (“ %d \n ”, i ) ; }

We Can Count Backwards, Too for ( i = 9 ; i >= 0 ; i = i - 1 ) { printf (“ %d \n ”, i ) ; }

We Can Count By 2’s ... or 7’s … or Whatever for ( i = 0 ; i < 10 ; i = i + 2 ) { printf ( “ %d\ n ”, i ) ; }

The for can contain arithmetic expressions x = 2; y =10; for ( j = x; j <= 4 * x * y; j = j + y / x ) { printf(“%d \n”, j ); } ------------------- is the same as: ------------------- for ( j = 2; j <= 80; j = j + 5 ) { printf(“%d \n”, j ); }

Flowchart of the for structure i = 1 true counter <= 10 i = i + 1; printf(“%d”, counter); body of the loop may be many statements increment the control variable false

The do-while Repetition Structure { statement(s) } while ( condition ) ; The body of a do-while is ALWAYS executed at least once. Is this true of a while loop? What about a for loop?

Example do { printf (“Enter a positive number: ”) ; scanf (“%d”, &num) ; if ( num <= 0 ) printf (“\nThat is not positive. Try again\n”) ; } } while ( num <= 0 ) ;

The do/while structure counter = 1 Notice that the loop continuation condition is not executed until after the action is performed at least once. action true condition false

An Equivalent while Loop printf (“Enter a positive number: ”) ; scanf (“%d”, &num) ; while ( num <= 0 ) { printf (“\nThat is not positive. Try again\n”) ; scanf (“ %d ”, &num) ; } Notice that using a while loop in this case requires a priming read.

An Equivalent for Loop printf (“Enter a positive number: ”) ; scanf (“%d”, &num) ; for ( ; num <= 0 ; ) { printf (“\nThat is not positive. Try again \n ” ) ; printf (“Enter a positive number: ” ) ; scanf ( “ %d ”, &num) ; } A for loop is a very awkward choice here because the loop is event-controlled.

So, Which Type of Loop Should I Use? Use a for loop for counter-controlled repetition. Use a while or do-while loop for event-controlled repetition. Use a do-while loop when the loop must execute at least one time. Use a while loop when it is possible that the loop may never execute.

for loops for specified number of iterations printf (“Enter the number of students: ”) ; scanf (“%d”, &numStudents); /* we now “know” how many times to loop */ for (student = 1; student <= numStudents; student=student+1) { printf (“ Enter grade: ” ) ; etc. }

use while when a condition terminates your loop the use of a sentinel is a good example...your program doesn’t “know” when the SENTINEL will be encountered printf (“Enter grade: ” ) ; scanf (“%d”, &grade ) ; while (grade != SENTINEL) { etc. }

Nested Loops Loops may be nested (embedded) inside of each other. Actually, any control structure (sequence, selection, or repetition) may be nested inside of any other control structure. It is common to see nested for loops.

Nested for Loops for ( i = 1; i < 5; i = i + 1 ) { for ( j = 1; j < 3; j = j + 1 ) if ( j % 2 == 0 ) printf (“O”) ; } else printf (“X”) ; printf (“\n”) ; How many times is the “if” statement executed? What is the output ?

The break Statement The break statement can be used in while, do-while, and for loops to cause premature exit of the loop. THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

Example break in a for Loop #include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) break ; } printf (“%d ”, i) ; printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ; OUTPUT: 1 2 3 4 Broke out of loop at i = 5.

Example break in a for Loop #include <stdio.h> int main ( ) { int x, y; for ( x = 1; x < 3; x = x + 1 ) { for ( y = 1; y < 9; y = y + 1) { printf(“x=%d y=%d \n”,x ,y ) ; if ( y = = 3 ) { break ; } return 0 ; OUTPUT: x = 1 y = 1 x = 1 y = 2 x = 1 y = 3 x = 2 y = 1 x = 2 y = 2 x = 2 y = 3

The continue Statement The continue statement can be used in while, do-while, and for loops. It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop. THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

Example continue in a for Loop #include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) continue ; } printf (“%d ”, i) ; printf ( “ \n Done. \n ” ) ; return 0 ; OUTPUT: 1 2 3 4 6 7 8 9 Done.

Nested for loops How many times is the ‘if’ statement executed? for (i = 1; i < 5; i = i+1 ) { for (j = 1; j < 3; j = j +1) if (j % 2 == 0) printf (“O”); } else printf (“X”); printf ( “ \n ” ) ; How many times is the ‘if’ statement executed? What is the output ??