Lec 05 Iteration Loop
Lec 05 Iteration Loop
Autumn 2020
Iterations and
Loops
Loops
Counter-controlled repetition
Definite repetition - know how many times loop will execute
Control variable used to count repetitions
Sentinel-controlled repetition
Indefinite repetition
Used when number of repetitions not known
Sentinel value indicates "end of data"
Example
counter ← 1, sum ← 0
false
counter < 6
Read 5 integers true
and display the input n
their sum
sum ← sum + n
counter = counter + 1
output sum
Example
“WRONG INPUT”
input m
true
m>49 “PASS”
false
“FAIL”
input m
false
m<0 || m>100
true
“WRONG INPUT”
input m
true
m>49 “PASS”
false
“FAIL”
Looping: while statement
while (expression)
statement;
while (expression) {
Block of statements;
}
The condition to be tested is any expression enclosed in parentheses. The
expression is evaluated, and if its value is non-zero, the statement is
executed. Then the expression is evaluated again and the same thing
repeats. The loop terminates when the expression evaluates to 0.
Looping: while statement
while (expression)
False
statement; expression
True
while (expression) {
statement
Block of statements; (loop body)
}
Looping: while statement
while (expression)
False
statement; expression
True
while (expression) {
statement
Block of statements; (loop body)
}
The condition to be tested is any expression enclosed in parentheses. The
expression is evaluated, and if its value is non-zero, the statement is
executed. Then the expression is evaluated again and the same thing
repeats. The loop terminates when the expression evaluates to 0.
Example
int i = 1, n;
scanf(“%d”, &n);
while (i <= n) {
printf (“Line no : %d\n”,i);
i = i + 1;
}
Example
int weight;
scanf(“%d”, &weight);
while ( weight > 65 ) {
printf ("Go, exercise, ");
printf ("then come back. \n");
printf ("Enter your weight: ");
scanf ("%d", &weight);
}
Sum of first N natural numbers
int main() {
int N, count, sum;
scanf (“%d”, &N) ;
sum = 0;
count = 1;
while (count <= N) {
sum = sum + count;
count = count + 1;
}
printf (“Sum = %d\n”, sum) ;
return 0;
}
SUM = 12 + 22 + 32 + …+ N2
int main() {
int N, count, sum;
scanf (“%d”, &N) ;
sum = 0;
count = 1;
while (count <= N) {
sum = sum + count count;
count = count + 1;
}
printf (“Sum = %d\n”, sum) ;
return 0;
}
Compute GCD of two numbers
int main() { 12 ) 45 ( 3
int A, B, temp; 36
scanf (“%d %d”, &A, &B); 9 ) 12 ( 1
if (A > B) {
9
temp = A; A = B; B = temp;
3 ) 9 ( 3
}
9
while ((B % A) != 0) {
temp = B % A; 0
B = A;
A = temp; Initial: A=12, B=45
} Iteration 1: temp=9, B=12,A=9
Iteration 2: temp=3, B=9, A=3
printf (“The GCD is %d”, A);
B % A = 0 GCD is 3
return 0;
}
Double your money
Suppose your Rs 10000 is earning interest at 1%
per month. How many months until you double
your money ?
int main() {
double my_money = 10000.0;
int n=0;
while (my_money < 20000.0) {
my_money = my_money * 1.01;
n++;
}
printf (“My money will double in %d months.\n”,n);
return 0;
}
Maximum of positive Numbers
int main() {
double max = 0.0, next;
printf (“Enter positive numbers, end with 0 or a
negative number\n”);
scanf(“%lf”, &next);
while (next > 0) {
if (next > max) max = next;
scanf(“%lf”, &next);
}
printf (“The maximum number is %lf\n”, max) ;
return 0;
}
Find the sum of digits of a number
int main()
{
int n, sum=0;
scanf (“%d”, &n);
while (n != 0) {
sum = sum + (n % 10);
n = n / 10;
}
printf (“The sum of digits of the number is %d \n”, sum);
return 0;
} digit-sum.c
Thank You!