Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
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.
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. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
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.
Structured Program Development in C
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
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.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Computer Programming Control Structure
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 7:
CISC105 – General Computer Science Class 4 – 06/14/2006.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 7 JavaScript: Control Statements, Part 1
CHAPTER 6: REPETITION AND LOOP STATEMENTS
JavaScript: Control Statements I
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Control Structure Senior Lecturer
Lecture 4A Repetition Richard Gesick.
Repetition and Loop Statements
3 Control Statements:.
CPS125 Week
EPSII 59:006 Spring 2004.
Presentation transcript:

Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University

Control Structures  Remember, in the very first lecture we talked about how algorithms are put together, and discussed sequential, conditional (selection) and iterative instructions. Sequential instructions o do them in the order given Conditional instructions o do them if a condition is true Iterative instructions o do them while a condition is true  So far, we’ve examined ways to translate sequential and conditional instructions we have in our algorithms into C programs. Today we’re going to see what C provides us to be able to carry out iterative instructions.

Motivation example In Lab 2 Task 1, we wrote a program that does the following: ◦ Carries out following task 10 times:  Reads a character from the input file  Prints corresponding ASCII value to the output file  Update statistics on numLines, numAlpha, and numDigits. ◦ Prints out the final statistics to an output file ch = read_character(infile); ascii = determine_ascii_value(ch); print_int(outfile_ascii, ascii); numLines= number_lines(ch, numLines); numDigits = number_digits(ch, numDigits); numAlpha = number_alphas(ch, numAlpha);

Motivation example (cont’d) In Lab 2 Task 1, we wrote a program that does the following: ◦ Carries out following task 10 times:  Reads a character from the input file  Prints corresponding ASCII value to the output file  Update statistics on numLines, numAlpha, and numDigits. ◦ Prints out the final statistics to an output file int counter = 0; while (counter < 10) { ch = read_character(infile); ascii = determine_ascii_value(ch); print_int(outfile_ascii, ascii); numLines= number_lines(ch, numLines); numDigits = number_digits(ch, numDigits); numAlpha = number_alphas(ch, numAlpha); counter += 1; }

5 How to decide when a loop is needed Sometimes, problem statement explicitly tells you to carry out a procedure a number of times, like in the case of the motivation example: read and process 10 characters. My rule of thumb is: if you need to do something more than 3 times, consider using a loop statement. Sometimes, problem statement doesn’t imply the need for a loop, but the algorithm you design to solve the problem, may require a loop. Think about the Greatest Common Divisor (GCD) problem (introduced in Lecture 1). Are any steps repeated in your algorithm? ◦ No  No loop required ◦ Yes  Do you know in advance how many steps are repeated?  No  Use a conditional loop  Yes  Use a counting loop

Loop patterns Counting loop ( for or while ): executes a fixed number of times Sentinel-controlled or Endfile-Controlled loop ( for or while ): process data until a special value is encountered, e.g., end-of-file Input validation loop ( do-while ): repeatedly accept interactive input until a value within a specified range is entered General conditional loop ( while, for ): repeatedly process data until a desired condition is met C. Hundhausen, A. O’Fallon

7 Counter Loops (1) Pseudocode for a counter loop: Set loop counter to 0 while (loop counter < final count) … (Do data processing) add 1 to loop counter endwhile

C. Hundhausen, A. O’Fallon8 Counter Loops (2) Implementing Counter Loops : the while loop while ( ) { }

C. Hundhausen, A. O’Fallon9 Counter Loops (3) Notes on while loops: ◦ is evaluated at beginning of loop. If it evaluates to true, the loop body is executed. If it evaluates to false, control shifts to first statement after loop body ◦ contains one or more C statements ◦ After last statement in is executed, control is shifted back to beginning of loop, and is re-evaluated. ◦ “Progress” must be made within the loop. That is, something must be done so that eventually evaluates to false. Otherwise we have an “infinite loop”

Counter Loops (4) int counter = 0; while (counter < 10) { ch = read_character(infile); ascii = determine_ascii_value(ch); print_int(outfile_ascii, ascii); numLines= number_lines(ch, numLines); numDigits = number_digits(ch, numDigits); numAlpha = number_alphas(ch, numAlpha); counter = counter + 1; } repetition condition initializing the counter advancing the counter

C. Hundhausen, A. O’Fallon11 Counter Loops (5) Another alternative for implementing Counter Loops : the for loop for ( ; ; ) { }

C. Hundhausen, A. O’Fallon12 Counter Loops (6) Notes on for loops: ◦ statement initializes the loop control variables before loop is executed the first time ◦ is tested at beginning of loop. If it is true, loop is executed. ◦ contains one or more C statements ◦ After last statement in is executed, control is shifted back to beginning of loop. Then, is executed. Finally, is re- evaluated. ◦ As with while loops, the must define “progress.” That is, something must be done so that eventually evaluates to false. Otherwise we have an “infinite loop”

Counter Loops (7) int counter; for(counter = 0; counter< 10; counter = counter +1) { ch = read_character(infile); ascii = determine_ascii_value(ch); print_int(outfile_ascii, ascii); numLines= number_lines(ch, numLines); numDigits = number_digits(ch, numDigits); numAlpha = number_alphas(ch, numAlpha); } initialization repetition conditionupdate expression

C. Hundhausen, A. O’Fallon14 Aside: Compound Assignment Operators Notice that the s in loops are often of the form: count = count + 1 C defines special assignment operators to define statements of this form more compactly: ExpressionEquivalent expression count = count +incrcount += incr count = count - incrcount -= incr count = count * factorcount *= factor count = count / factorcount /= factor count = count % factorcount %= factor

C. Hundhausen, A. O’Fallon15 Aside: Increment and Decrement Operators (1) The ++ and -- operators take a single variable as their operands. The side effect of the operator is to increment or decrement its operand by one: ◦ count++ has the effect of count = count + 1 ◦ count-- has the effect of count = count – 1 Note: ++ and -- can be placed either before or after their variable operator: ◦ Pre-increment or pre-decrement (e.g., ++count, --count ): value of expression is value of variable after the increment or decrement is applied ◦ Post-increment of post-decrement (e.g., count++, count-- ): value of expression is value of variable before the increment or decrement is applied

C. Hundhausen, A. O’Fallon16 Aside: Increment and Decrement Operators (2) You try it: What are the values of i, j, and k after each of the following statements is executed? int i, j, k; i = 2; j = 3 + i++; k = i; i *= ++k + j--; i /= k j;

C. Hundhausen, A. O’Fallon17 Counter Loops (8) Notice that we can rewrite our previous while loop with these new operators: int counter = 0; while (counter < 10) { ch = read_character(infile); ascii = determine_ascii_value(ch); print_int(outfile_ascii, ascii); numLines= number_lines(ch, numLines); numDigits = number_digits(ch, numDigits); numAlpha = number_alphas(ch, numAlpha); counter++; }

C. Hundhausen, A. O’Fallon18 Counter Loops (9) Notice that we can also rewrite our previous for loop with these new operators: int counter; for(counter = 0; counter< 10; counter++) { ch = read_character(infile); ascii = determine_ascii_value(ch); print_int(outfile_ascii, ascii); numLines= number_lines(ch, numLines); numDigits = number_digits(ch, numDigits); numAlpha = number_alphas(ch, numAlpha); }

Example problem – solution with while Problem statement: Find average of 10 floating point numbers taken from the user int counter = 0; double sum = 0; double number = 0.0; printf ("Enter 10 floating point numbers for me:"); while (counter <10) { scanf("%lf", &number); sum += number; counter++; } printf("Average of them is: %lf\n", sum/counter); Accumulating a value

Example problem- solution with for Problem statement: Find average of10 floating point numbers taken from the user int counter = 0; double sum = 0; double number = 0.0; printf ("Enter 10 floating point numbers for me:"); sum = 0; for (counter = 0; counter <10; counter++) { scanf("%lf", &number); sum += number; } printf("Average of them is: %lf\n", sum/counter); Notice the use of comma operator

21 It’s OK to have loop increments other than 1!

C. Hundhausen, A. O’Fallon22 Next Lecture… We'll discuss some additional loop patterns: ◦ Conditional loops ◦ Sentinel-controlled loops & end-file- controlled loops ◦ Flag-controlled loops

23 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.