LOOP
(Part 2)
for
while
do-while
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;
}
TK1913-C Programming
Example
Write a program which does the following:
Reads 5 integers and displays the sum of all
integers
Input example: 3
Output example: 16
TK1913-C Programming
Recall the flowchart
counter 1, sum 0
false
counter < 6
true
input x
sumsum+ x
counter++
output sum
TK1913-C Programming
Note the initial
value of i
and condition
i 1, sum 0
false
i< 6
true
How many times
does the loop get
executed?
input x
sumsum+ x
i++
output sum
TK1913-C Programming
i 0, sum 0
false
i< 6
true
How many times
does the loop get
executed?
input x
sumsum+ x
i++
output sum
TK1913-C Programming
i 0, sum 0
false
i< 5
true
How many times
does the loop get
executed?
input x
sumsum+ x
i++
output sum
TK1913-C Programming
The C statements:
int x, sum, i;
sum = 0;
for (i = 0; i < 5; i++) {
scanf(%d,&x);
sum = sum + x;
}
printf(%d,sum);
TK1913-C Programming
i 0, sum 0
false
i< 5
true
input x
sumsum+ x
i++
output sum
TK1913-C Programming
int x, sum, i;
sum = 0;
for (i = 0; i < 5; i++) {
scanf(%d,&x);
sum = sum + x;
}
printf(%d,sum);
9
for statement
???
Example:
for ( num = 1; num <= 3; num++ )
printf(%d\t, num);
1
num
TK1913-C Programming
10
for statement
1
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
num
TK1913-C Programming
11
for statement
1
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
num
TK1913-C Programming
12
for statement
1
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
num
TK1913-C Programming
13
for statement
2
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
num
TK1913-C Programming
14
for statement
2
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
num
TK1913-C Programming
15
for statement
2
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
bil
TK1913-C Programming
16
for statement
3
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
num
TK1913-C Programming
17
for statement
3
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
num
TK1913-C Programming
18
for statement
3
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
TK1913-C Programming
num
19
for statement
4
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
TK1913-C Programming
num
20
for statement
4
Example:
for (num = 1; num <= 3; num++ )
printf(%d\t, num);
TK1913-C Programming
num
21
for
while
do-while
TK1913-C Programming
22
Loop: while
Condition is tested first
Loop is controlled by condition or a counter
Syntax
while (condition)
statement;
Or
while (condition) {
statement;
statement;
}
TK1913-C Programming
23
Recall this example:
Given an exam marks as input, display the
appropriate 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
TK1913-C Programming
24
input m
false
m<0 || m>100
true
WRONG INPUT
input m
Exercise:
Convert this
flowchart to a
C program
true
m>49
PASS
false
FAIL
TK1913-C Programming
25
int marks;
scanf(%d,&marks);
while (marks<0) | | (marks>100) {
printf(WRONG INPUT);
scanf(%d,&marks);
}
if (marks>49) {
printf(PASS);
else
printf(FAIL);
}
TK1913-C Programming
Double
Selection
26
Exercise
Given a set of integers
with the last one being 999
Display the summation of
all the integers.
Draw the
flowchart for
this problem
Input example:
1 3 23
999
Output example:
Sum = 27
TK1913-C Programming
27
Sentinel-controlled loop
#include <stdio.h>
sum=0
void main() {
input x
int sum, x;
sum = 0;
false
scanf(%d, &x);
x!=999
while (x != 999) {
true
sum = sum + x;
sumsum+x
scanf(%d, &x);
}
input x
output sum
TK1913-C Programming
printf(The sum : %d\n, sum);
}
28
int sum, x;
sum = 0;
scanf(%d, &x);
3!=
999
!=!=
999
999
999
!= 999
while (x != 999) { 123
sum = sum + x;
scanf(%d, &x);
}
printf(\nThe sum : %d\n, sum);
_
1
23
sum
?
999
23
1
3
?
4+23
0+1
1+3
27
0
1
4
999
The sum : 27
TK1913-C Programming
29
Do-while Loop
Statements in the loop are executed first (at least
onc, and condition is tested last
Loop is controlled by a condition or counter
Syntax
do {
statement;
statement;
} while (condition);
statement;
TK1913-C Programming
30
do-while statement
start
???
67
end
Example :
printf(Input start and end value : );
scanf(%d %d, &start, &end);
do {
printf(%c (%d)\n, start, start);
start++;
66 <= 67
67
} while (start <= end) ; 68
_Input start and end value : 65
_ 67
67_
A (65)
_ (66)
B
_ (67) TK1913-C Programming
C
_
???
65
66
67
68
31
_ is an even number.
0
number._Print iff even !
continue statement
_ is an even number.
2
number._Print iff even !
_ is an even number.
4
number._Print iff even !
i
0
1
5
6
4
3
2
Example:
for ( i = 0; i <= 5; i++ ) {
if ( i % 2 )
continue;
else
printf(%d is an even number. , i);
printf(Print iff even ! \n);
}
i <= 5
i%2
64
0<=
1
2
3
5<=
<=
55
9false
5true
true
TK1913-C Programming
0
1
32
Now what can you
conclude about
continue?
TK1913-C Programming
33
Input a value between 1-7: 5
_
4
1
break statement
2
3
3YeePee!
Im out of the loop!
printf(Input a value between 1 7: );
scanf(%d,&value);
for (i = 1; i <= 7; i++) {
if ( i = = value )
break;
printf(\n%d,i);
i
1
2
3
4
}
printf(YeePee! Im out of the loop!\n);
TK1913-C Programming
???
4
value
i <= 7
43
21<=
<=
<=777true
true
true
i == value
24
3
1==
==44false
false
true
34
Now what can you
conclude about
break?
TK1913-C Programming
35
End of
st
1
half of the semester
Yes !! Thats all?
Whats next???
1 week HOLIDAY
on the way
TK1913-C Programming
36