7 - ICS103 Programming in C Lecture 7 Repetition Structures 1 Overview Repetition in Programs Counting Loops Using while statement Compound assignment

7 - ICS103 Programming in C Lecture 7 Repetition Structures...

This preview shows page 1 - 10 out of 47 pages.

1 ICS103 Programming in C Lecture 7: Repetition Structures
Image of page 1
2 Overview Repetition in Programs Counting Loops Using while statement Compound assignment operators Using for statement Increment and Decrement Operators Conditional Loops sentinel-Controlled loops Nested loop Do-While loop Flag-Controlled loop Hand Tracing the code Comparing Double numbers Debugging your code
Image of page 2
3 Repetition in Programs We have learned how to write code that chooses between multiple alternatives. It is also useful to be able to write code that repeats an action. Writing out a solution to a specific case of problem can be helpful in preparing you to define an algorithm to solve the same problem in general. After you solve the specific case, you need to determine whether loops will be required in the general algorithm and if so which loop structure to choose from.
Image of page 3
4 Flow Diagram of Loop Choice Process
Image of page 4
5 Counting Loops The loop shown below in pseudo code is called a counter- controlled loop (or counting loop) because its repetition is managed by a loop control variable whose value represents a count. Set loop control variable to an initial value of 0 While loop control variable < final value ... //Do something multiple times Increase loop control variable by 1. We use a counter-controlled loop when we can determine prior to loop execution exactly how many loop repetitions will be needed to solve the problem.
Image of page 5
6 The While Statement This slide shows a program fragment that computes and displays the gross pay for seven employees. The loop body is the compound statements (those between { and }). The loop repetition condition controls the while loop. count_emp = 0; while (count_emp < 7) { printf("Hours> "); scanf("%d",&hours); printf("Rate> "); scanf("%lf",&rate); pay = hours * rate; printf("Pay is $%6.2f\n", pay); count_emp = count_emp + 1; } printf("\nAll employees processed\n"); loop repetition condition
Image of page 6
7 While Statement General form: While (loop repetition condition) { //Steps to perform. These should eventually //result in condition being false } Syntax of the while Statement: Initialization. i.e. count_emp = 0; Testing. i.e. count_emp < 7 Updating i.e. count_emp = count_emp + 1; The above steps must be followed for every while loop. If any of these are skipped it may produce an infinite loop
Image of page 7
8 General While Loops In the above example we had count_emp < 7 , but we may have more or less than 7 employees. To make our program fragment more general we should use a printf/scanf to get the number of employees and store it is num_emp . Now we can have count_emp < num_emp and our code is more general.
Image of page 8
9 Computing Sum If we want to compute , we need to go 1+2+3+... +100 We can use a while loop.
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 47 pages?

  • Spring '10
  • baleh
  • sentinel value, SENTINEL, Decrement Operators

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture