EEE122 LabSheet4
EEE122 LabSheet4
Lab Sheet 4
Iteration Statements in C
Outcomes
After finishing this lab students should be able to ...
4. use the break and continue statements to alter the flow of control.
Contents
1 C - Iteration 1
1.1 while loop in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 for loop in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 do...while loop in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 nested loop in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4 Programming Examples 8
5 Practice session 10
6 Lab Assignments 11
1 C - Iteration
In C programming generally statements are executed sequentially. But often programmer needs to
execute a block of code several number of times. A iteration/loop statement allows us to execute a
statement or group of statements multiple times.
C programming language provides the following types of loops to handle looping requirements.
EEE 122: Structured Programming Laboratory 2
1. while loop : Repeats a statement or group of statements while a given condition is true. It tests
the condition before executing the loop body.
2. for loop : Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
3. do...while loop : It is more like a while statement, except that it tests the condition at the end
of the loop body.
4. nested loops : You can use one or more loops inside any other while, for, or do..while loop.
Flow Diagram
Start
false
condition
true
statements
end
Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
/* local variable definition */
int a = 10;
/ * while loop e x e c u t i o n * /
while ( a < 20 ) {
printf ( ” value o f a : %d\n” , a ) ;
a++;
}
return 0;
}
Syntax
f o r ( initialization ; condition ; increment ) {
statement ( s ) ;
}
Flow Diagram
Start
initialization
false
condition
true
statements
increment
end
Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
int a ;
/ * f o r loop e x e c u t i o n * /
for ( a = 10; a < 20; a = a + 1 ) {
printf ( ” value o f a : %d\n” , a ) ;
}
return 0;
}
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least
one time.
Syntax
do {
statement ( s ) ;
} while ( condition ) ;
EEE 122: Structured Programming Laboratory 4
Flow Diagram
Start
statements
true
condition
false
end
Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
/* local variable definition */
int a = 10;
/ * do loop e x e c u t i o n * /
do {
printf ( ” value o f a : %d\n” , a ) ;
a = a + 1;
} while ( a < 20 ) ;
return 0;
}
or ,
while ( condition ) {
while ( condition ) {
statement ( s ) ;
}
statement ( s ) ;
}
or ,
do {
statement ( s ) ;
do {
statement ( s ) ;
} while ( condition ) ;
} while ( condition ) ;
EEE 122: Structured Programming Laboratory 5
Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
/* local variable definition */
int i , j ;
f o r ( i = 2 ; i <100; i++) {
f o r ( j = 2 ; j <= ( i / j ) ; j++) {
i f ( ! ( i%j ) )
break ; / / i f f a c t o r found , not prime
if (j > (i/j) )
printf ( ”%d i s prime ” , i ) ;
}
}
return 0;
}
Flow Diagram
Start
statements
true
condition break
false
end
EEE 122: Structured Programming Laboratory 6
Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
/* local variable definition */
int a = 10;
/ * while loop e x e c u t i o n * /
while ( a < 20 ) {
printf ( ” value o f a : %d\n” , a ) ;
a++;
i f ( a > 15) {
/ * t e r m i n a t e t h e loop using break s t a t e m e n t * /
break ;
}
}
return 0;
}
For the for loop, continue statement causes the conditional test and increment portions of the loop to
execute. For the while and do...while loops, continue statement causes the program control to pass
to the conditional tests.
Syntax
continue ;
Flow Diagram
Start
statements
true
condition continue
false
end
Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
/* local variable definition */
int a = 10;
/ * do loop e x e c u t i o n * /
do {
EEE 122: Structured Programming Laboratory 7
i f ( a == 1 5 ) {
/ * skip the i t e r a t i o n * /
a = a + 1;
continue ;
}
printf ( ” value o f a : %d\n” , a ) ;
a++;
} while ( a < 20 ) ;
return 0;
}
Flow Diagram
Start
label1 statements1
goto
label2 statements2
label3
label3 statements2
end
Example
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
/* local variable definition */
int a = 10;
/ * do loop e x e c u t i o n * /
LOOP : do {
i f ( a == 1 5 ) {
/ * skip the i t e r a t i o n * /
a = a + 1;
goto LOOP ;
}
printf ( ” value o f a : %d\n” , a ) ;
a++;
} while ( a < 20 ) ;
return 0;
}
EEE 122: Structured Programming Laboratory 8
i n t main ( void ) {
for ( ; ; ) {
printf ( ” This loop w i l l run f o r e v e r . \ n” ) ;
}
return 0;
}
4 Programming Examples
Example: 1
Description: Write a program in C which takes a number from user and test it whether it is a prime
number or not.
Source Code Output
Enter the number:11
# i n c l u d e < s t d i o . h> 11 is a prime number.
i n t main ( void ) {
i n t i , n , flag ;
printf ( ” \ nEnter t h e number : ” ) ;
scanf ( ”%d” ,&n ) ;
f o r ( i =2; i<=n −1; i++) {
i f ( n%i==0)
flag =1;
}
i f ( flag ==1)
printf ( ” \n%d i s not a prime number . ” , n ) ;
else
printf ( ” \n%d i s a prime number . ” , n ) ;
return 0;
}
Example: 2
Description: Write a program in C which finds the factorial of any number.
Source Code Output
Enter a number: 10
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
Factorial is: 3628800
i n t number =1;
long i n t answer ;
printf ( ” \ nEnter a number : ” ) ;
scanf ( ”%d” , &number ) ;
i f ( number <0)
printf ( ” \nNo value o f f a c t o r i a l f o r n e g a t i v e value . ” ) ;
e l s e i f ( number ==0)
printf ( ” \ n F a c t o r i a l i s : 1 ” ) ;
e l s e i f ( number >= 1 ) {
answer =1;
while ( number >0) {
answer = answer * number −−;
}
printf ( ” F a c t o r i a l i s : %ld \n” , answer ) ;
}
return 0;
}
EEE 122: Structured Programming Laboratory 9
Example: 3
Description:Write a program in C which converts a binary number to a decimal number.
Source Code Output
Enter the number in binary:
# include <stdio . h> 10010101101
i n t main ( void ) { The decimal equivalent is:1997
long i n t n , k , rem , d , a =1 , dec =0;
printf ( ” \ nEnter t h e number i n b i n a r y : ” ) ;
scanf ( ”%ld ” ,&n ) ;
f o r ( k=n ; k >0; k / = 1 0 ) {
rem=k%10;
d=rem * a ;
dec+=d ;
a *=2;
}
printf ( ” \nThe decimal e q u i v a l e n t i s :%d” , dec ) ;
return 0;
}
Example: 4
Description: Write a program in C which finds the gcf of two given integers.
Source Code Output
Enter the numbers:16 8
# i n c l u d e < s t d i o . h> gcf is:8
i n t main ( void ) {
int a , b , c ;
printf ( ” \ nEnter t h e numbers : \ n” ) ;
scanf ( ”%d %d” ,&a ,&b ) ;
while ( b ! = 0 ) {
c=a%b ;
a=b ;
b=c ;
}
printf ( ” \ ngcf i s :%d” , a ) ;
return 0;
}
Example: 5
Description: Write a program in C which finds the sum of the series: 12 + 22 + 32 + .... + n2 , n taking
from keyboard. You are not allowed to use algebraic summation low.
Source Code Output
How many numbers?5
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
Sum=55
long i n t n , sum =0 , i ;
printf ( ” \nHow many numbers ? ” ) ;
scanf ( ”%ld ” ,&n ) ;
f o r ( i =0; i<=n ; i++) {
sum+=i * i ;
}
printf ( ” \nSum=%l d ” , sum ) ;
return 0;
}
EEE 122: Structured Programming Laboratory 10
5 Practice session
Sl Source Code
Practice 1
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
i n t i =10;
do {
printf ( ” i=%d\n” , i ) ;
i=i −3;
} while ( i ) ;
return 0;
}
Practice 2
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
int i ;
f o r ( i =1; i <10; i++) {
i f ( i==3)
continue ;
printf ( ”%d ” , i ) ;
}
return 0;
}
Practice 3
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
do {
printf ( ” Hello t h e r e \n” ) ;
} while ( 4 < 1 ) ;
return 0;
}
Practice 4
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
i n t i , j =10;
f o r ( ; i=j ; j−=2)
printf ( ”%d\n” , j ) ;
return 0;
}
EEE 122: Structured Programming Laboratory 11
6 Lab Assignments
1. Write a program to print all prime numbers from 1 to 300.
1
12
123
1234
12345
.
.
.
4. Write a C program to find the value of n Pr . n and r are taken from the keyboard .
Acknowledgment
First OBE version, prepared by: Second Update , prepared by:
B.K.M. Mizanur Rahman, Nazmul Alam,
Assistant Professor, Part Time Faculty,
Department of EEE, UIU Department of EEE, UIU