Lecture 03 - Repetition
Lecture 03 - Repetition
March 2022 CSE102 Computer Programming 3 March 2022 CSE102 Computer Programming 4
1
3/9/2022
General format:
set counter to 0 /* initialization */
while counter < final value /* test */
do one or more things /* loop body */
increase counter by one /* updating */
March 2022 CSE102 Computer Programming 5 March 2022 CSE102 Computer Programming 6
March 2022 CSE102 Computer Programming 7 March 2022 CSE102 Computer Programming 8
2
3/9/2022
} • Total payroll
March 2022 CSE102 Computer Programming 9 March 2022 CSE102 Computer Programming 10
(continued)
March 2022 CSE102 Computer Programming 11 March 2022 CSE102 Computer Programming 12
3
3/9/2022
In general: In general:
var = var op exp var op= exp
March 2022 CSE102 Computer Programming 13 March 2022 CSE102 Computer Programming 14
• Syntax:
for (intialization expression;
loop repetition condition;
update expression)
statement;
March 2022 CSE102 Computer Programming 15 March 2022 CSE102 Computer Programming 16
4
3/9/2022
March 2022 CSE102 Computer Programming 19 March 2022 CSE102 Computer Programming 20
5
3/9/2022
March 2022 CSE102 Computer Programming 21 March 2022 CSE102 Computer Programming 22
March 2022 CSE102 Computer Programming 23 March 2022 CSE102 Computer Programming 24
6
3/9/2022
(continued)
March 2022 CSE102 Computer Programming 25 March 2022 CSE102 Computer Programming 26
March 2022 CSE102 Computer Programming 27 March 2022 CSE102 Computer Programming 28
7
3/9/2022
March 2022 CSE102 Computer Programming 31 March 2022 CSE102 Computer Programming 32
8
3/9/2022
March 2022 CSE102 Computer Programming 33 March 2022 CSE102 Computer Programming 34
printf(……); Algorithm:
for (scanf(“%d”,&score); Initialize sum to zero
score != SENTINEL; Read the first value
scanf(“%d”,&score)) { while end of file is not reached
sum += score; Add value to sum
printf(…….); Read the next value
}
March 2022 CSE102 Computer Programming 35 March 2022 CSE102 Computer Programming 36
36
9
3/9/2022
March 2022 CSE102 Computer Programming 37 March 2022 CSE102 Computer Programming 38
March 2022 CSE102 Computer Programming 39 March 2022 CSE102 Computer Programming 40
10
3/9/2022
March 2022 CSE102 Computer Programming 41 March 2022 CSE102 Computer Programming 42
March 2022 CSE102 Computer Programming 43 March 2022 CSE102 Computer Programming 44
11
3/9/2022
March 2022 CSE102 Computer Programming 45 March 2022 CSE102 Computer Programming 46
March 2022 CSE102 Computer Programming 47 March 2022 CSE102 Computer Programming 48
12
3/9/2022
• What is displayed by the following program segments, assuming m is 3 and n is 5? • for statements and while statements evaluate loop repetition condition before
the first execution of the loop body.
• Pretest is usually undesirable
• when there may be no data items to process
• when the initial value of the loop control variable is outside its expected range.
• Sometimes loop must execute at least once
• Ex: interactive input
March 2022 CSE102 Computer Programming 49 March 2022 CSE102 Computer Programming 50
March 2022 CSE102 Computer Programming 51 March 2022 CSE102 Computer Programming 52
13
3/9/2022
March 2022 CSE102 Computer Programming 53 March 2022 CSE102 Computer Programming 54
• Execution results
March 2022 CSE102 Computer Programming 55 March 2022 CSE102 Computer Programming 56
56
14
3/9/2022
Do While Statement and Flag Controled Loops Do While Statement and Flag Controled Loops
• Do they behave similarly? Why? • Which of the following code is better way to implement a sentinel-controlled loop?
Why?
do {
scanf(“%d”, &num);
scanf(“%d”, &num); /* process num */ scanf(“%d”, &num);
while (num != SENT) { do {
} while (num != SENT); while (num != SENT) {
/* process num */ /* process num */
scanf(“%d”, &num);
scanf(“%d”, &num); scanf(“%d”, &num); if (num != SENT)
} } /* process num */
} while (num != SENT);
March 2022 CSE102 Computer Programming 57 March 2022 CSE102 Computer Programming 58
58
March 2022 CSE102 Computer Programming 59 March 2022 CSE102 Computer Programming 60
59
15
3/9/2022
Program Variables
energy_resrc /* usable solar energy available in coldest month
• Two data files (Btus obtained from 1 Ft2 of collecting area) */
• hdd.txt contains numbers representing the average heating degree days for
each months. Problem Outputs
heat_loss /* Btus of heat lost by structure in coldest month */ collect_area /*
• solar.txt contains the average solar insolation for each month approximate size Ft2 of collecting area needed */
March 2022 CSE102 Computer Programming 61 March 2022 CSE102 Computer Programming 62
Case Study:
• Algorithm
1. Determine the coldest month and the average heating degree days for this
month.
2. Find the average daily solar insolation per Ft2 for the coldest month.
3. Get the other problem inputs from the user :
heating_req, efficiency, floor_space.
1. Estimate the collecting area needed.
2. Display results.
March 2022 CSE102 Computer Programming 63 March 2022 CSE102 Computer Programming 64
16
3/9/2022
March 2022 CSE102 Computer Programming 65 March 2022 CSE102 Computer Programming 66
March 2022 CSE102 Computer Programming 67 March 2022 CSE102 Computer Programming 68
17
3/9/2022
March 2022 CSE102 Computer Programming 69 March 2022 CSE102 Computer Programming 70
March 2022 CSE102 Computer Programming 71 March 2022 CSE102 Computer Programming 72
18
3/9/2022
March 2022 CSE102 Computer Programming 73 March 2022 CSE102 Computer Programming 74
March 2022 CSE102 Computer Programming 75 March 2022 CSE102 Computer Programming 76
19
3/9/2022
March 2022 CSE102 Computer Programming 77 March 2022 CSE102 Computer Programming 78
March 2022 CSE102 Computer Programming 79 March 2022 CSE102 Computer Programming 80
79 80
20
3/9/2022
21