CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Similar documents
Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

Iteration. CSE / ENGR 142 Programming I. while loops. Chapter 5. Motivating Loops. Loop to Add 5 Numbers 1996 UW CSE H - 1

Repetition and Loop Statements Chapter 5

CSE / ENGR 142 Programming I

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

n Group of statements that are executed repeatedly while some condition remains true

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

Dept. of CSE, IIT KGP

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

C++ Programming: From Problem Analysis to Program Design, Third Edition

Loops / Repetition Statements

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

A Look Back at Arithmetic Operators: the Increment and Decrement

Looping. Arizona State University 1

Chapter 4: Control structures. Repetition

Display Input and Output (I/O)

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter 4: Control structures

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Repetition Structures II

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Welcome! COMP s1. Programming Fundamentals

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

CS110D: PROGRAMMING LANGUAGE I

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Overview. CS 201 Repetition. Endfile-controlled loops. What is the Output? Nested Loops. Loops. Debzani Deb

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Decision Making and Loops

Repetition CSC 121 Fall 2014 Howard Rosenthal

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Structured Programming. Dr. Mohamed Khedr Lecture 9

REPETITION CONTROL STRUCTURE LOGO

Introduction. C provides two styles of flow control:

Loops / Repetition Statements

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Building Java Programs

Building Java Programs

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

STUDENT LESSON A12 Iterations

Chapter 5: Loops and Files

Programming for Electrical and Computer Engineers. Pointers and Arrays

Chapter 5: Control Structures

Lecture 10. Daily Puzzle

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Programming for Engineers Iteration

C Programming Basics

Laboratory 5: Implementing Loops and Loop Control Strategies

Writing to and reading from files

Fundamentals of Programming Session 7

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Chapter 13 Control Structures

Lecture 4 CSE July 1992

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter Goals. Contents LOOPS

Control structures in C. Going beyond sequential

Control Structures II. Repetition (Loops)

CS 106 Introduction to Computer Science I

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

CpSc 1111 Lab 5 Formatting and Flow Control

slicing An example of slicing Consider the following example:

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Java Coding 3. Over & over again!

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Computer Programming & Problem Solving ( CPPS )

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Why Is Repetition Needed?

3 The L oop Control Structure

LECTURE 3 C++ Basics Part 2

Prepared by: Shraddha Modi

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Use of scanf. scanf("%d", &number);

LOOPS. Repetition using the while statement

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Lab ACN : C++ Programming Exercises

Module 4: Decision-making and forming loops

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Principles of Computer Science I

CpSc 1111 Lab 4 Formatting and Flow Control

Transcription:

CPE 101 slides adapted from UW course Lecture (9): Iteration Overview Concepts this lecture Iteration - repetitive execution Loops and nested loops while statements for statements 2000 UW CSE H1-1 H1-2 Chapter 5 Read Sections 5.1-5.6, 5.10 5.1 Introduction 5.2-5.3 While statement 5.4 For statement 5.5-5.6 Loop design 5.7 Nested Loops 5.11 Common errors H1-3 An Old Friend: Fahrenheit to Celsius #include <stdio.h> int main(void) { double fahrenheit, celsius; printf("enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = (fahrenheit - 32.0) * 5.0 / 9.0; printf("that equals %f degrees Celsius.", celsius); return 0; H1-4 H1-1

What s Wrong with Fahrenheit/Celsius Program? H1-5 What s Wrong with Fahrenheit/Celsius Program? User has to rerun the program for every new temperature Wouldn t it be nice if the program could process repeated requests? Program ends immediately if user types a bad input Wouldn t it be nice the program politely asked the user again (and again, etc. if necessary)? H1-6 One More Type of Control Flow Sometimes we want to repeat a block of code. This is called a loop. Loops A loop is a repeated ( iterated ) sequence of statements Like conditionals, loops (iteration) give us a huge increase in the power of our programs Alert: loops are harder to master than if statements Even experienced programmers often make subtle errors when writing loops H1-7 H1-8 H1-2

Motivating Loops Problem: add 4 numbers entered at the keyboard. int sum; int x1, x2, x3, x4; printf( Enter 4 numbers: ); scanf( %d%d%d%d, &x1, &x2, &x3, &x4); sum = x1 + x2 + x3 + x4; Motivating Loops Problem: add 4 numbers entered at the keyboard. int sum; int x1, x2, x3, x4; printf( Enter 4 numbers: ); scanf( %d%d%d%d, &x1, &x2, &x3, &x4); sum = x1 + x2 + x3 + x4; H1-9 This works perfectly! But... what if we had 14 numbers? or 40? or 4000? H1-10 Finding Repeated Code The key to using loops to solve a problem is to discover steps that can be repeated Our first algorithm for adding four numbers had no repeated statements at all But it does have some repetition buried in it. Let s rework the algorithm to make the repetition more explicit Add 4 Numbers, Repetitively int sum, x; sum = 0; printf( Enter 4 numbers: ); scanf( %d, &x); sum = sum + x; scanf( %d, &x); sum = sum + x; scanf( %d, &x); sum = sum + x; H1-11 scanf( %d, &x); sum = sum + x; H1-12 H1-3

Loop to Add 4 Numbers More General Solution int sum, x; sum = 0; printf( Enter 4 numbers: ); scanf( %d, &x); sum = sum + x; scanf( %d, &x); sum = sum + x; scanf( %d, &x); sum = sum + x; scanf( %d, &x); sum = sum + x; int sum, x; int count; sum = 0; printf( Enter 4 numbers: ); count = 1; while (count <= 4) { scanf( %d, &x); sum = sum + x; count = count + 1; int sum, x, count; int number_inputs; /* Number of inputs */ sum = 0; printf( How many numbers? ); scanf( %d, &number_inputs); printf( Enter %d numbers:, number_inputs); count = 1; while ( count <= number_inputs ) { scanf( %d, &x); sum = sum + x; count = count + 1; H1-13 H1-14 while Statement Syntax while ( condition ) { statement1; statement2;... Loop condition Loop body: Any statement, or a compound statement H1-15 Compute 7! What is 1 * 2 * 3 * 4 * 5 * 6 * 7? ( seven factorial ) x = 1 * 2 * 3 * 4 * 5 * 6 * 7; printf ( %d, x ) ; H1-16 H1-4

Compute 7! What is 1 * 2 * 3 * 4 * 5 * 6 * 7? ( seven factorial ) x = 1 * 2 * 3 * 4 * 5 * 6 * 7; printf ( %d, x ) ; Bite size pieces: More Regular: As a loop: x = 1; x = 1; i = 2; x = 1; x = x * 2; x = x * i; i = i + 1; i = 2; x = x * 3; x = x * i; i = i + 1; while ( i <= 7 ) { x = x * 4; x = x * i; i = i + 1; x = x * i; x = x * 5; x = x * i; i = i + 1; i = i + 1; x = x * 6; x = x * i; i = i + 1; x = x * 7; x = x * i; i = i + 1; H1-17 while Loop Control Flow x = 1 ; i = 2 ; i <= 7? no yes x = x * i ; i = i + 1 ; H1-18 Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x i 7? A? 1 B 2 1 C 2 1 T D 2 2 E 3 2 C 3 2 T... C 6 120 T D 6 720 E 7 720 C 7 720 T D 7 5040 E 8 5040 C 8 5040 F G (Print 5040) H1-19 Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ line i x x = 1 ; /* A */ A? 1 i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ H1-20 H1-5

Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x A? 1 B 2 1 Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x i 7? A? 1 B 2 1 C 2 1 T H1-21 H1-22 Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x i 7? A? 1 B 2 1 C 2 1 T D 2 2 E 3 2 H1-23 Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x i 7? A? 1 B 2 1 C 2 1 T D 2 2 E 3 2 C 3 2 T H1-24 H1-6

Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x i 7? A? 1 B 2 1 C 2 1 T D 2 2 E 3 2 C 3 2 T... C 7 720 T D 7 5040 E 8 5040 H1-25 Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x i 7? A? 1 B 2 1 C 2 1 T D 2 2 E 3 2 C 3 2 T... C 7 720 T D 7 5040 E 8 5040 C 8 5040 F H1-26 Tracing the Loop /* What is 1 * 2 * 3 *...*7 */ x = 1 ; /* A */ i = 2 ; /* B */ while ( i <= 7 ) { /* C */ x = x * i ; /* D */ i = i + 1 ; /* E */ /* F */ printf ( %d, x ) ; /* G */ line i x i 7? A? 1 B 2 1 C 2 1 T D 2 2 E 3 2 C 3 2 T... C 7 720 T D 7 5040 E 8 5040 C 8 5040 F G (Print 5040) H1-27 Double Your Money /* Suppose your $1,000 is earning interest at 5% per year. How many years until you double your money? */ H1-28 H1-7

Double Your Money /* Suppose your $1,000 is earning interest at 5% per year. How many years until you double your money? */ my_money = 1000.0; n = 0; while ( my_money < 2000.0 ) { my_money = my_money *1.05; n = n + 1; Average Inputs printf ( Enter values to average, end with -1.0 \n ) ; sum = 0.0 ; count = 0 ; sentinel scanf ( %lf, &next ) ; while ( next!= -1.0 ) { sum = sum + next ; count = count + 1; scanf ( %lf, &next ) ; if (count > 0) printf( The average is %f. \n, printf( My money will double in %d years., n); H1-29 H1-30 sum / (double) count ); Printing a 2-D Figure How would you print the following diagram? repeat 3 times print a row of 5 stars repeat 5 times print Nested Loop #define ROWS 3 #define COLS 5 H1-31 H1-32 It seems as if a loop within a loop is needed. H1-8

Nested Loop outer loop: print 3 rows inner loop: print one row H1-33 row: col: H1-34 row: 1 col: row: 1 col: 1 H1-35 H1-36 H1-9

row: 1 col: 1 row: 1 col: 1 2 * H1-37 * H1-38 row: 1 col: 1 2 row: 1 col: 1 2 3 * * H1-39 * * H1-40 H1-10

row: 1 col: 1 2 3 row: 1 col: 1 2 34 * * * H1-41 * * * H1-42 row: 1 col: 1 2 34 row: 1 col: 1 2 34 5 * * * * H1-43 * * * * H1-44 H1-11

row: 1 col: 1 2 34 5 row: 1 col: 1 2 34 5 6 H1-45 H1-46 row: 1 2 col: 1 2 34 5 6 row: 1 2 col: 1 2 34 5 6 1 H1-47 H1-48 H1-12

row: 1 2 col: 1 2 34 5 6 1 row: 1 2 col: 1 2 34 5 6 1 2 * H1-49 * H1-50 row: 1 2 col: 1 2 34 5 6 1 2 row: 1 2 col: 1 2 34 5 6 1 2 3 * * H1-51 * * H1-52 H1-13

row: 1 2 col: 1 2 34 5 6 1 2 3 row: 1 2 col: 1 2 34 5 6 1 2 34 * * * H1-53 * * * H1-54 row: 1 2 col: 1 2 34 5 6 1 2 34 row: 1 2 col: 1 2 34 5 6 1 2 34 5 * * * * H1-55 * * * * H1-56 H1-14

row: 1 2 col: 1 2 34 5 6 1 2 34 5 row: 1 2 col: 1 2 34 5 6 1 2 34 5 6 H1-57 H1-58 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 H1-59 H1-60 H1-15

row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 * H1-61 * H1-62 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 3 * * H1-63 * * H1-64 H1-16

row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 3 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 34 * * * H1-65 * * * H1-66 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 34 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 34 5 * * * * H1-67 * * * * H1-68 H1-17

row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 34 5 row: 1 2 3 col: 1 2 34 5 6 1 2 34 5 6 1 2 34 5 6 H1-69 H1-70 row: 1 2 3 4 col: 1 2 34 5 6 1 2 34 5 6 1 2 34 5 6 Print a Multiplication Table 1 2 3 1 1 2 3 H1-71 2 2 4 6 3 3 6 9 4 4 8 12 H1-72 H1-18

Print a Multiplication Table 1 2 3 1 2 3 1 1 2 3 1 1 * 1 1 * 2 1 * 3 2 2 4 6 2 2 * 1 2 * 2 2 * 3 3 3 6 9 3 3 * 1 3 * 2 3 * 3 4 4 8 12 4 4 * 1 4 * 2 4 * 3 H1-73 1 2 3 4 1 2 3 1 2 3 2 4 6 3 6 9 4 8 12 while (col <= 3) { printf( %4d, 2 * col ); printf( \n ); Print Row 2 row number H1-74 Nested Loops while (row <= 4) { while (col <= 3) { printf( %4d, row * col ); printf( \n ); Print 4 rows Print one row H1-75 Loop row col 1 1 print 1 2 print 2 3 print 3 print \n 2 1 print 2 2 print 4 3 print 6 print \n H1-76 H1-19

Loop row col 1 1 print 1 2 print 2 3 print 3 print \n 2 1 print 2 2 print 4 3 print 6 print \n row col 3 1 print 3 2 print 6 3 print 9 print \n 4 1 print 4 2 print 8 3 print 12 print \n Notes About Loop Conditions They offer all the same possibilities as conditions in if-statements Can use &&,,! Condition is reevaluated each time through the loop A common loop condition: checking the number of times through the loop H1-77 H1-78 Counting Loops A common loop condition: checking the number of times through the loop A for Loop /* What is 1 * 2 * 3 *... * n? */ Requires keeping a counter This pattern occurs so often there is a separate statement type based on it: the x = 1 ; i = 2 ; while ( i <= n ) { x = x * i ; i = i+1; for-statement H1-79 H1-80 printf ( %d, x ) ; H1-20

A for Loop /* What is 1 * 2 * 3 *... * n? */ x = 1 ; i = 2 ; while ( i <= n ) { x = x * i ; i = i+1; printf ( %d, x ) ; x = 1 ; for ( i = 2 ; i <= n ; i = i+1 ) { x = x * i ; printf ( %d, x) ; H1-81 for Statement Syntax for ( initialization; condition; update expression) { statement1; statement2;... H1-82 for Loop Control Flow Initialization ** (fri) for Loops vs while Loops Any for loop can be written as a while loop These two loops mean exactly the same thing: for (initialization; condition; update) statement; no Condition yes For Loop Body initialization; while (condition) { statement; update; Update Expression H1-83 H1-84 H1-21

Counting in for Loops /* Print n asterisks */ for ( count = 1 ; count <= n ; count = count + 1 ) { printf ( * ) ; Counting in for Loops /* Print n asterisks */ for ( count = 1 ; count <= n ; count = count + 1 ) { printf ( * ) ; H1-85 /* Different style of counting */ for ( count = 0 ; count < n ; count = count + 1 ) { printf ( * ); H1-86 outer loop: print 3 rows 3 Rows of 5 as a Nested for Loop #define ROWS 3 #define COLS 5... for ( row <= ROWS ; row = row + 1 ) { for ( col = 1 ; col <= COLS ; col = col + 1 ) { printf( ); inner loop: print one row H1-87 Yet Another 2-D Figure How would you print the following diagram? For every row ( row = 1, 2, 3, 4, 5 ) Print row stars H1-88 H1-22

Solution: Another Nested Loop Yet One More 2-D Figure #define ROWS 5... int row, col ; for ( row = 1 ; row <= ROWS ; row = row + 1 ) { for ( col = 1 ; col <= row ; col = col + 1) { printf( ) ; H1-89 H1-90 Yet Another Nested Loop #define ROWS 5... int row, col ; for ( row = 0 ; row < ROWS ; row = row + 1 ) { for ( col = 1 ; col <= row ; col = col + 1) printf( ) ; for ( col = 1 ; col <= ROWS row ; col = col + 1) printf( ) ; H1-91 Some Loop Pitfalls while ( sum < 10 ) ; sum = sum + 2; H1-92 H1-23

Some Loop Pitfalls while ( sum < 10 ) ; sum = sum + 2; for ( i = 0; i <= 10; i = i + 1); sum = sum + i ; Some Loop Pitfalls while ( sum < 10 ) ; sum = sum + 2; for ( i = 0; i <= 10; i = i + 1); sum = sum + i ; for ( i = 1; i!= 10 ; i = i + 2 ) sum = sum + i ; H1-93 H1-94 Double Danger double x ; for ( x = 0.0 ; x < 10.0 ; x = x + 0.2 ) printf( %.18f, x) ; Seems harmless... H1-95 Double Danger What you expect: What you might get: 0.000000000000000000 0.000000000000000000 0.200000000000000000 0.200000000000000000 0.400000000000000000 0.400000000000000000...... 9.000000000000000000 8.999999999999999997 9.200000000000000000 9.199999999999999996 9.400000000000000000 9.399999999999999996 9.600000000000000000 9.599999999999999996 9.800000000000000000 9.799999999999999996 H1-96 9.999999999999999996 H1-24

Use ints as Loop Counters Counting in Loops int i ; Counting up by one or down by one: double x ; for ( i = 0 ; i < 50 ; i = i + 1 ) { x = (double) i / 5.0 ; printf( %.18f, x) ; H1-97 for ( i = 1 ; i <= limit ; i = i+1 ) {... times_to_go = limit; while ( times_to_go > 0 ) { times_to_go = times_to_go - 1; H1-98 Counting Up or Down by 1 This pattern is so common there is special jargon and notation for it To "increment:" increase (often by 1) To "decrement:" decrease (often by 1) Handy Shorthand x++ Used by itself, x++ means the same as x = x+1 x-- means the same as x = x-1 Very often used with loop counters: for ( i=1 ; i <= limit ; i++ ) {... x-- C operators: Post-increment ( x++ ): add 1 times_to_go = limit; while ( times_to_go > 0 ) {... H1-99 H1-100 Post-decrement ( x-- ): subtract 1 times_to_go--... H1-25

Surgeon General's Warning ++ and -- are unary operators. Pre-increment (++x) and pre-decrement (--x) exist, too. In this course, use ++ and -- only in isolation. Don't combine these with other operators in expressions! E.g., don't try Iteration Summary General pattern: Initialize, test, do stuff, repeat... while and "for" are equally general in C Use for when initialize/test/update are closely related and simple, especially when counting x = y++ / (3 * --x--) H1-101 H1-102 For completeness The do while loop construct: Do { block of C statements while (condition); Why use a do while? ideal for flag controlled loops you scan for input and then decide whether to continue. H1-103 H1-104 H1-26

Looking Ahead We ll talk more about how to design loops We ll learn about user defined functions We ll discuss complex conditional expressions Can be used with loops as well as in conditional statements We ll eventually see arrays, a powerful new way of organizing data H1-105 H1-27