Control Flow: Looping Control Flow: Looping: Programming & Data Structures Programming & Data Structures
Control Flow: Looping Control Flow: Looping: Programming & Data Structures Programming & Data Structures
Pallab Dasgupta
Professor, Dept. of Computer Sc. & Engg.,
Engg.,
Indian Institute of Technology Kharagpur
Sentinel
Controlled
Condition
Controlled
Counter Controlled
1, 2, 3, 4,
, 4, 3, 2, 1
Dept. of CSE, IIT KGP
Read 5 integers
and display the
value of their
summation.
counter 1, sum 0
counter < 6
true
input n
sum sum + n
counter++
output sum
false
Condition-controlled Loop
Condition-Controlled Loop
input m
WRONG INPUT
true
m<0 || m>100
false
Condition-controlled
loop with its condition
being tested at the end
true
m>49
false
FAIL
PASS
input m
false
m<0 || m>100
true
WRONG INPUT
input m
Condition-controlled
Conditionloop with its
condition being
tested first
true
m>49
false
FAIL
PASS
Sentinel-Controlled Loop
Input Example:
30 16
42
Output Example:
Sum = 88
Average = 29.33
-9
Sentinel
Value
while loop
while (expression)
statement
expression
T
while (i < n) {
printf (Line no : %d.\n,i);
i++;
}
statement
(loop body)
while Statement
The while statement is used to carry out looping
operations, in which a group of statements is executed
repeatedly, as long as some condition remains
satisfied.
while (condition)
statement_to_repeat;
while (condition) {
statement_1;
...
statement_N;
}
Note:
The while-loop will not be entered if the loop-control
expression evaluates to false (zero) even before the first
iteration.
break can be used to come out of the while loop.
Dept. of CSE, IIT KGP
while :: Examples
int weight;
while ( weight > 65 ) {
printf ("Go, exercise, ");
printf ("then come back. \n");
printf ("Enter your weight: ");
scanf ("%d", &weight);
}
START
READ N
SUM = 0
COUNT = 1
SUM = SUM + COUNT
COUNT = COUNT + 1
NO
IS
COUNT > N?
YES
OUTPUT SUM
STOP
Maximum of inputs
printf (Enter positive numbers to max, end with 1.0\n);
max = 0.0;
count = 0;
scanf(%f, &next);
while (next != 1.0) {
if (next > max)
max = next;
count++;
scanf(%f, &next);
}
printf (The maximum number is %f\n, max) ;
*****
*****
*****
repeat 3 times
print a row of 5
stars
repeat 5 times
print *
Nested Loops
#define ROWS 3
#define COLS 5
...
row=1;
while (row <= ROWS) {
/* print a row of 5 *s */
...
row++;
}
row=1;
while (row <= ROWS) {
/* print a row of 5 *s */
col=1;
while (col <= COLS) {
printf (* );
col++;
}
printf(\n);
row++;
}
outer
loop
inner
loop
do-while statement
statement
F
expression
T
int main () {
char echo ;
do {
scanf (%c, &echo);
printf (%c,echo);
} while (echo != \n) ;
}
for Statement
The for statement is the most commonly
used looping structure in C.
General syntax:
for ( expr1; expr2; expr3) statement
expr1 (init) : initialize parameters
expr2 (test): test condition, loop continues if satisfied
expr3 (update): used to alter the value of the parameters
after each iteration
statement (body): body of the loop
expr1;
while (expr2) {
statement
expr3;
}
<= N; count++)
sum = sum + count;
2-D Figure
Print
*****
*****
*****
#define ROWS 3
#define COLS 5
....
for (row=1; row<=ROWS; row++) {
for (col=1; col<=COLS; col++) {
printf(*);
}
printf(\n);
}
#define ROWS 5
....
int row, col;
for (row=1; row<=ROWS; row++) {
for (col=1; col<=row; col++) {
printf(* );
}
printf(\n);
}
For - Examples
Problem 1: Write a For statement that computes the sum of all odd
numbers between 1000 and 2000.
Problem 2: Write a For statement that computes the sum of all
numbers between 1000 and 10000 that are divisible by 17.
Problem 3: Printing square problem but this time make the square
hollow.
Problem 4: Print
*****
****
***
**
*
Problem 4 : solution
Print
*****
****
***
**
*
#define ROWS 5
....
int row, col;
for (row=0; row<ROWS; row++) {
for (col=1; col<=row; col++)
printf(" ");
for (col=1; col<=ROWS-row; col++)
printf("* ");
printf ("\n");
}
while (1) {
statements
}
do {
statements
} while (1);
An Example
#include <stdio.h>
int main() {
int fact, i;
fact = 1; i = 1;
while ( i<10 ) {
/* run loop break when fact >100*/
fact = fact * i;
if ( fact > 100 ) {
printf ("Factorial of %d above 100", i);
break;
/* break out of the while loop */
}
i ++ ;
}
}
for structure
expression3 is evaluated, then expression2 is evaluated.
Some Examples
START
READ N
SUM = 0
COUNT = 1
SUM = SUM + COUNT
COUNT = COUNT + 1
NO
IS
COUNT > N?
YES
OUTPUT SUM
STOP
NO
IS
COUNT > N?
YES
OUTPUT SUM
STOP
Example 5: 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;
}
START
READ N
SUM = 0
COUNT = 1
SUM = SUM + COUNT COUNT
COUNT = COUNT + 1
NO
IS
COUNT > N?
YES
OUTPUT SUM
STOP
int main () {
int N, count, prod;
scanf (%d, &N) ;
prod = 1;
for (count=0;count < N; count++) {
prod =prod*count;
printf (Factorial = %d\n, prod) ;
return 0;
}
COUNT = COUNT + 1
NO
IS
COUNT > N?
YES
OUTPUT PROD
STOP
NO
IS
COUNT > N?
YES
OUTPUT SUM
STOP
int main () {
float x, term, sum;
int n, count;
scanf (%d, &x) ;
scanf (%d, &n) ;
term = 1.0; sum = 0;
for (count = 0; count < n; count++) {
sum += term;
term = x/count;
}
printf (%f\n, sum) ;
}
Dept. of CSE, IIT KGP
NO
IS
TERM < 0.0001?
YES
OUTPUT SUM
STOP
int main () {
float x, term, sum;
int n, count;
scanf (%d, &x) ;
scanf (%d, &n) ;
term = 1.0; sum = 0;
for (count = 0; term<0.0001; count++) {
sum += term;
term *= x/count;
}
printf (%f\n, sum) ;
}
Dept. of CSE, IIT KGP
More efficient??
#include <stdio.h>
main()
{
int n, i=3;
scanf (%d, &n);
while (i < sqrt(n)) {
if (n % i == 0) {
printf (%d is not a prime \n, n);
exit;
}
i = i + 2;
}
printf (%d is a prime \n, n);
}
12 ) 45 ( 3
36
9 ) 12 ( 1
9
3 ) 9 ( 3
9
0
A=12, B=45
Iteration 1: temp=9, B=12,A=9
Iteration 2: temp=3, B=9, A=3
B % A = 0 GCD is 3
single character
decimal integer
floating-point number
string terminated by null character
hexadecimal integer
Examples:
printf (The average of %d and %d is %f, a, b, avg);
printf (Hello \nGood \nMorning \n);
printf (%3d %3d %5d, a, b, a*b+2);
printf (%7.2f %5.1f, x, y);
String I/O:
Will be covered later in the class.