{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

This preview shows pages 1–10. Sign up to view the full content.

1 ICS103 Programming in C Lecture 7: Repetition Structures

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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
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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Flow Diagram of Loop Choice Process
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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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
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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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.
9 Computing Sum If we want to compute , we need to go 1+2+3+... +100 We can use a while loop.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 47

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

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online