CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.

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.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
© 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.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
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.
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)
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Structured Programming
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.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
 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;
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
The ‘while’ loop ‘round and ‘round we go.
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.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
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.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
Chapter 4 – C Program Control
- Standard C Statements
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?
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
The while Looping Structure
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 4 - Program Control
The while Looping Structure
UMBC CMSC 104 – Section 01, Fall 2016
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
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:

CMSC 104, Version 9/011 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 , 4.8, 4.9

CMSC 104, Version 9/012 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 ; }

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

CMSC 104, Version 9/014 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: “) ; scanf(“%d”, &value) ; }

CMSC 104, Version 9/015 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.

CMSC 104, Version 9/016 #include int main () { int i = 1 ;initialization of loop control variable /* count from 1 to 100 */ while ( i < 101 ) test of loop termination condition { printf (“%d “, i) ; i = i + 1 ;modification of loop control } variable return 0 ; } The 3 Parts of a Loop

CMSC 104, Version 9/017 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) { initializationmodification }test

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

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

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

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

CMSC 104, Version 9/0112 The do-while Repetition Structure do { 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?

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

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

CMSC 104, Version 9/0115 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.

CMSC 104, Version 9/0116 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. o Use a do-while loop when the loop must execute at least one time. o Use a while loop when it is possible that the loop may never execute.

CMSC 104, Version 9/0117 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.

CMSC 104, Version 9/0118 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 ?

CMSC 104, Version 9/0119 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.

CMSC 104, Version 9/0120 Example break in a for Loop #include 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: Broke out of loop at i = 5.

CMSC 104, Version 9/0121 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.

CMSC 104, Version 9/0122 Example continue in a for Loop #include int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) { if (i == 5) { continue ; } printf (“%d ”, i) ; } printf (“\nDone.\n”) ; return 0 ; } OUTPUT: Done.