slide 08b - control structure_loop
TRANSCRIPT
-
8/10/2019 Slide 08b - Control Structure_Loop
1/36
LOOP
(Part 2)
for
whiledo-while
1
-
8/10/2019 Slide 08b - Control Structure_Loop
2/36
TK1913-C Programming 2
Loop :for
Condition is tested first
Loop is controlled by a counter
Syntaxes
for (initial value ; condition; update counter)statement;
Or
for (initial value ; condition; update counter) {
statement;
statement;
}
-
8/10/2019 Slide 08b - Control Structure_Loop
3/36
TK1913-C Programming 3
Example
Write a program which does the following:
Reads 5 integers and displays the sum of all
integers
Input example: 3 6 4 1 2
Output example: 16
-
8/10/2019 Slide 08b - Control Structure_Loop
4/36
TK1913-C Programming 4
Recall the flowchart
counter 1, sum 0
counter < 6
sumsum+ x
false
true
counter++
output sum
input x
-
8/10/2019 Slide 08b - Control Structure_Loop
5/36
TK1913-C Programming 5
Note the initial
value of iand condition
i 1, sum 0
i < 6
sumsum+ x
false
true
i++
output sum
input xHow many times
does the loop get
executed?
-
8/10/2019 Slide 08b - Control Structure_Loop
6/36
TK1913-C Programming 6
i 0, sum 0
i < 6
sumsum+ x
false
true
i++
output sum
input xHow many times
does the loop get
executed?
-
8/10/2019 Slide 08b - Control Structure_Loop
7/36
TK1913-C Programming 7
i 0, sum 0
i < 5
sumsum+ x
false
true
i++
output sum
input xHow many times
does the loop get
executed?
-
8/10/2019 Slide 08b - Control Structure_Loop
8/36
TK1913-C Programming 8
The C statements:
int x, sum, i;
sum = 0;
for (i = 0; i < 5; i++) {scanf(%d,&x);
sum = sum + x;
}printf(%d,sum);
-
8/10/2019 Slide 08b - Control Structure_Loop
9/36
TK1913-C Programming 9
i 0, sum 0
i < 5
sumsum+ x
false
true
i++
output sum
input x
int x, sum, i;
sum = 0;
for (i = 0; i < 5; i++) {scanf(%d,&x);
sum = sum + x;
}printf(%d,sum);
-
8/10/2019 Slide 08b - Control Structure_Loop
10/36
-
8/10/2019 Slide 08b - Control Structure_Loop
11/36
TK1913-C Programming 11
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
12/36
TK1913-C Programming 12
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
13/36
TK1913-C Programming 13
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
14/36
-
8/10/2019 Slide 08b - Control Structure_Loop
15/36
TK1913-C Programming 15
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
16/36
TK1913-C Programming 16
for statement
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
17/36
TK1913-C Programming 17
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
18/36
TK1913-C Programming 18
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
19/36
TK1913-C Programming 19
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
20/36
TK1913-C Programming 20
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
21/36
TK1913-C Programming 21
Example:for (num = 1; num
-
8/10/2019 Slide 08b - Control Structure_Loop
22/36
TK1913-C Programming 22
for
while
do-while
-
8/10/2019 Slide 08b - Control Structure_Loop
23/36
TK1913-C Programming 23
Loop: while
Condition is tested first
Loop is controlled by condition or a counter
Syntax
while (condition)statement;
Or
while (condition) {
statement;
statement;}
-
8/10/2019 Slide 08b - Control Structure_Loop
24/36
TK1913-C Programming 24
Recall this example:
Given an exam marks as input, display theappropriate message based on the rules below:
If marks is greater than 49, display PASS,
otherwise display FAIL
However, for input outside the 0-100 range,
display WRONG INPUT and prompt the
user to input again until a valid input is
entered
-
8/10/2019 Slide 08b - Control Structure_Loop
25/36
TK1913-C Programming 25
false
true
input m
m100
m>49 PASS
FAIL
true
false
WRONG INPUT
input m
Exercise:
Convert this
flowchart to aC program
-
8/10/2019 Slide 08b - Control Structure_Loop
26/36
TK1913-C Programming 26
int marks;
scanf(%d,&marks);while (marks100) {
printf(WRONG INPUT);
scanf(%d,&marks);
}
if (marks>49) {
printf(PASS);
elseprintf(FAIL);
}
Double
Selection
-
8/10/2019 Slide 08b - Control Structure_Loop
27/36
TK1913-C Programming 27
Exercise
Given a set of integerswith the last one being 999
Display the summation ofall the integers.
Input example:
1 3 23 999
Output example:
Sum = 27
Draw the
flowchart for
this problem
S ti l t ll d l
-
8/10/2019 Slide 08b - Control Structure_Loop
28/36
TK1913-C Programming 28
Sentinel-controlled loop
true
x!=999
sumsum+x
false
input x
sum=0
input x
output sum
#include
void main() {int sum, x;
sum = 0;
scanf(%d, &x);
while (x != 999) {
sum = sum + x;
scanf(%d, &x);
}printf(The sum : %d\n, sum);
}
-
8/10/2019 Slide 08b - Control Structure_Loop
29/36
TK1913-C Programming 29
int sum, x;
sum = 0;
scanf(%d, &x);while (x != 999) {
sum = sum + x;
scanf(%d, &x);
}printf(\nThe sum : %d\n, sum);
?
?x
sum 0
1
_1
1 != 999
0+11
1 3
33 != 999
1+34
1 3 23
2323 != 999
4+2327
1 3 23 999
999999 != 999
The sum : 27
-
8/10/2019 Slide 08b - Control Structure_Loop
30/36
TK1913-C Programming 30
Do-while Loop
Statements in the loop are executed first (at leastonc, and condition is tested last
Loop is controlled by a condition or counter
Syntax
do {
statement;
statement;
} while (condition);statement;
??? ???
65 67
66
67
68
-
8/10/2019 Slide 08b - Control Structure_Loop
31/36
TK1913-C Programming 31
do-while statement
Example :printf(Input start and end value : );
scanf(%d %d, &start, &end);
do {
printf(%c (%d)\n, start, start);
start++;
} while (start
-
8/10/2019 Slide 08b - Control Structure_Loop
32/36
TK1913-C Programming 32
continuestatement
Example:for ( i = 0; i
-
8/10/2019 Slide 08b - Control Structure_Loop
33/36
TK1913-C Programming 33
Now what can you
conclude about
continue?
Input a value between 1-7:
Input a value between 1-7: 4
Input a value between 1-7: 4
Input a value between 1-7: 4
Input a value between 1-7: 4
Input a value between 1-7: 5
-
8/10/2019 Slide 08b - Control Structure_Loop
34/36
TK1913-C Programming 34
breakstatement
printf(Input a value between 1 7: );
scanf(%d,&value);
for (i = 1; i
-
8/10/2019 Slide 08b - Control Structure_Loop
35/36
TK1913-C Programming 35
Now what can you
conclude about
break?
-
8/10/2019 Slide 08b - Control Structure_Loop
36/36
TK1913 C Programming 36
End of 1sthalf of the semester
Yes !! Thats all?Whats next???
1 week HOLIDAY
on the way