chapter 5
play

Chapter 5 CSCE150A CSCE150A 5.1 Repetition in Programs Computer - PDF document

Chapter 5 CSCE150A CSCE150A 5.1 Repetition in Programs Computer Science & Engineering 150A 5.2 Counting Loops and the While Statement Introduction Problem Solving Using Computers Introduction While Loop While Loop 5.3 Computing a Sum


  1. Chapter 5 CSCE150A CSCE150A 5.1 Repetition in Programs Computer Science & Engineering 150A 5.2 Counting Loops and the While Statement Introduction Problem Solving Using Computers Introduction While Loop While Loop 5.3 Computing a Sum or a Product in a Loop Compound Compound Lecture 05 - Loops Assignment Assignment 5.4 The for Statement For Loop For Loop 5.5 Conditional Loops Loop Design Loop Design 5.6 Loop Design Nested Loops Nested Loops Stephen Scott 5.7 Nested Loops Do-While Do-While (Adapted from Christopher M. Bourke) Loop Loop 5.8 Do While Statement and Flag-Controlled Loops Programming Programming Tips Tips 5.10 How to Debug and Test 5.11 Common Programming Errors Fall 2009 1 / 54 2 / 54 Repetition in Programs Counting Loops CSCE150A CSCE150A A counter-controlled loop (or counting loop ) is a loop whose repetition is managed by a loop control variable whose value represents a Just as the ability to make decisions ( if-else selection statements) is an Introduction Introduction count. Also called a while loop. important programming tool, so too is the ability to specify the repetition While Loop While Loop of a group of operations. Compound Compound Assignment Assignment When solving a general problem, it is sometimes helpful to write a For Loop For Loop Set counter to an initial value of 0 1 solution to a specific case. Once this is done, ask yourself: Loop Design Loop Design while counter < someFinalV alue do 2 Nested Loops Nested Loops Block of program code Were there any steps that I repeated? If so, which ones? 3 Do-While Do-While Loop Loop Increase counter by 1 Do I know how many times I will have to repeat the steps? 4 Programming Programming Tips Tips If not, how did I know how long to keep repeating the steps? end 5 Algorithm 1: Counter-Controlled Loop 3 / 54 4 / 54 The C While Loop While Loop Syntax Syntax of the while Statement: CSCE150A CSCE150A This while loop computes and displays the gross pay for seven employees. The loop body is a compound statement (between brackets) The loop Initialize the loop control variable repetition condition controls the while loop. Introduction Introduction Without initialization, the loop control variable value is meaningless. While Loop While Loop 1 Test the loop control variable before the start of each loop repetition int count_emp = 0; // Set counter to 0 Compound Compound 2 while (count_emp < 7) // If count_emp < 7, do stmts Update the loop control variable during the iteration Assignment Assignment 3 { For Loop For Loop Ensures that the program progresses to the final goal 4 printf("Hours > "); Loop Design Loop Design 5 scanf("%d" ,&hours ); Nested Loops 6 Nested Loops 1 count = 1; printf("Rate > "); Do-While 7 scanf("%lf" ,&rate ); Do-While 2 while(count <= 10) Loop Loop 8 pay = hours * rate; 3 { Programming Programming 9 printf("Pay is $%6.2f\n", pay); Tips Tips 4 printf("Count = %d\n",count ); 10 count_emp = count_emp + 1; /* Increment count_emp */ 5 11 } count = count + 1; 12 printf("\nAll employees processed\n"); 6 } 5 / 54 6 / 54

  2. Common Programming Errors General While Loops CSCE150A CSCE150A Best to generalize code whenever possible Skipping crucial steps could lead to an infinite loop 1 int numEmployees , count_emp =0; Introduction Introduction Common error: forgetting to increment your loop control variable 2 printf("How many employees > "); While Loop While Loop Syntax error: misplaced semicolons 3 scanf("%d", &numEmployees ); Compound Compound Assignment Assignment 4 while(count_emp < numEmployees) For Loop For Loop 1 count = 1; 5 { Loop Design Loop Design 2 <= 10); ← WRONG while(count 6 . . . Nested Loops Nested Loops 3 { 7 count_emp = count_emp + 1; Do-While Do-While 4 printf("Count = %d\n",count ); Loop Loop 8 } 5 count = count + 1; Programming Programming Tips Tips 6 } Using numEmployees instead of the constant 7 allows our code to be more general. 7 / 54 8 / 54 While Loop Exercise While Loop Exercise Answer CSCE150A CSCE150A Exercise Write a while loop to compute the sum of natural numbers 1 to 100: 1 int sum = 0; Introduction Introduction 100 2 int i = 1; /* our loop control variable */ While Loop While Loop � i = 1 + 2 + · · · + 100 3 Compound Compound while (i <= 100) Assignment Assignment i =1 4 { For Loop For Loop 5 sum = sum + i; Generalize the loop so that the sum from 1 to any n can be computed. Loop Design Loop Design 6 i = i + 1; Nested Loops Nested Loops 7 } Do-While Do-While Steps to design: Loop Loop 8 printf("Sum is %d\n", sum); Programming Programming Tips Identify and define a loop control variable. Tips Write the syntax for the loop control structure Fill in the code used within the loop to compute the sum 9 / 54 10 / 54 While Loop Exercise While Loop Example II Answer: Generalized Instead of the sum of integers 1 to n , compute the product: CSCE150A CSCE150A 1 int sum = 0; 100 � i = 1 × 2 × . . . × 100 Introduction 2 int n = 100; /* general variable , may be Introduction While Loop 3 While Loop * changed or read from input */ i =1 Compound 4 Compound int i = 1; /* our loop control variable */ What changes need to be made? Assignment Assignment 5 while (i <= n) For Loop For Loop Variable names? 6 { Loop Design Loop Design Initialized variable value? 7 sum = sum + i; Nested Loops Nested Loops 8 i = i + 1; Operators? Do-While Do-While Loop Loop 9 } Programming Programming Note: this is the factorial function, 10 printf("Sum 1 to %d is %d\n", n, sum); Tips Tips n � n ! = i i =1 11 / 54 12 / 54

  3. While Loop Example II Program Failed Answer CSCE150A CSCE150A 1 int product = 1; 2 Introduction int n = 100; /* general variable , may be Introduction Run the previous program: it gives an answer of 0—why? While Loop 3 * changed or read from input */ While Loop Compound Compound 4 int i = 1; /* our loop control variable */ Debug your code: use a printf statement in the loop to see what Assignment Assignment 5 while (i <= n) intermediate values are computed: For Loop For Loop 6 { Loop Design Loop Design printf("i = %3d product = %d\n",i,product); 7 product = product * i; Nested Loops Nested Loops Check the answers with a calculator 8 i = i + 1; Do-While Do-While Loop Loop For what i does this program fail? 9 } Programming Programming 10 Tips printf("Product 1 to %d is %d\n", n, product ); Tips 13 / 54 14 / 54 Overflow Compound Assignment Operators CSCE150A CSCE150A Expressions such as variable = variable op expression; (where op is a C operator such as +,-,*,/, ) occur frequently We got the wrong answer for i = 13 , C provides several syntax shortcuts Introduction Introduction While Loop While Loop x = x + 1; and x += 1; are “equivalent” 13! = 6 , 227 , 020 , 800 Compound Compound Can do this with other operators (see table) Assignment Assignment We used a 32-bit integer to store product For Loop For Loop Maximum representable value is 2 31 = 2 , 147 , 483 , 648 Loop Design Loop Design Expression Shortcut Nested Loops Nested Loops When a number is too large (or too small!) to be represented by its x = x + 1; x += 1; Do-While Do-While type, overflow occurs (or underflow ) Loop Loop x = x - 1; x -= 1; Programming Programming More sophisticated solutions are available, but outside this course’s x = x * 5; x *= 5; Tips Tips scope x = x / 2; x /= 2; Table: Compound Assignment Operators 15 / 54 16 / 54 Compound Assignment Operators For Loops Example Revisited CSCE150A CSCE150A 1 int product = 1; Introduction 2 int n = 100; /* general variable , may be Introduction While Loop 3 While Loop * changed or read from input */ Compound 4 Compound int i = 1; /* our loop control variable */ Program Style Assignment Assignment 5 while (i <= n) For Loop For Loop Increment and Decrement Operators 6 { Loop Design Loop Design Increment and Decrement Other Than 1 7 product *= i; Nested Loops Nested Loops 8 i += 1; Do-While Do-While Loop Loop 9 } Programming Programming 10 printf("Product 1 to %d is %d\n", n, product ); Tips Tips 17 / 54 18 / 54

Recommend


More recommend