0% found this document useful (0 votes)
12 views11 pages

EEE122 LabSheet4

This document is a lab sheet for the Structured Programming Laboratory course at United International University, focusing on iteration statements in C programming. It covers various types of loops, including while, for, and do...while loops, as well as control statements like break and continue. Additionally, it includes examples and exercises to practice the concepts learned.

Uploaded by

moniruzzaman.if
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

EEE122 LabSheet4

This document is a lab sheet for the Structured Programming Laboratory course at United International University, focusing on iteration statements in C programming. It covers various types of loops, including while, for, and do...while loops, as well as control statements like break and continue. Additionally, it includes examples and exercises to practice the concepts learned.

Uploaded by

moniruzzaman.if
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

United International University

Dept. of Electrical and Electronic Engineering (EEE)

Course No. : EEE 122


Course Title: Structured Programming Laboratory

Lab Sheet 4
Iteration Statements in C
Outcomes
After finishing this lab students should be able to ...

1. use the while repetition statement to execute statements in a program repeatedly.

2. use the do...while repetition statements to execute statements repeatedly.

3. use the for statements to execute statements repeatedly

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

2 Loop Control Statements 5


2.1 break statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 continue statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 goto statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3 The Infinite Loop 8

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.

1.1 while loop in C


A while loop in C programming repeatedly executes a target statement as long as a given condition is
true.
Syntax
while ( condition ) {
statement ( s ) ;
}

Flow Diagram

Start

false
condition

true

statements

end

Fig: while loop flow chart

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;
}

1.2 for loop in C


A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
EEE 122: Structured Programming Laboratory 3

Syntax
f o r ( initialization ; condition ; increment ) {
statement ( s ) ;
}

Flow Diagram

Start

initialization

false
condition

true

statements

increment

end

Fig: for loop flow chart

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;
}

1.3 do...while loop in C


Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop
in C programming checks its condition at the bottom of the loop.

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

Fig: do...while loop flow chart

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;
}

1.4 nested loop in C


C programming allows to use one loop inside another loop. The following section shows a few
examples to illustrate the concept.
Syntax
f o r ( init ; condition ; increment ) {
f o r ( init ; condition ; increment ) {
statement ( s ) ;
}
statement ( s ) ;
}

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;
}

2 Loop Control Statements


Loop control statements change execution from its normal sequence. When execution leaves a scope,
all automatic objects that were created in that scope are destroyed.
C supports the following control statements.
1. break statement
2. continue statement
3. goto statement

2.1 break statements


The break statement in C programming has the following two usages
1. When a break statement is encountered inside a loop, the loop is immediately terminated and
the program control resumes at the next statement following the loop.
2. It can be used to terminate a case in the switch statement (covered in the next chapter).
Syntax
break ;

Flow Diagram

Start

statements

true
condition break

false

end
EEE 122: Structured Programming Laboratory 6

Fig: break statement flow chart

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;
}

2.2 continue statements


The continue statement in C programming works somewhat like the break statement. Instead of
forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.

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

Fig: continue statement flow chart

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;
}

2.3 goto statements


A goto statement in C programming provides an unconditional jump from the ’goto’ to a labeled
statement in the same function.
Syntax
goto label ;
..
.
label : statement ;

Flow Diagram

Start

label1 statements1

goto
label2 statements2
label3

label3 statements2

end

Fig: goto statement flow chart

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

3 The Infinite Loop


A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used
for this purpose. Since none of the three expressions that form the ’for’ loop are required, you can
make an endless loop by leaving the conditional expression empty.
Example
# i n c l u d e < s t d i o . h>

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.

2. Write a program to generate all combinations of 1, 2 and 3 using for loop.

3. Write a C program to print the following number pyramid:

1
12
123
1234
12345
.
.
.

Number of lines are taken from keyboard.

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

You might also like