Iterative Statements
Iterative Statements
Topic:
ITERATIVE STATEMENTS
Session -
INSTRUCTIONAL OBJECTIVES
This Session is designed to learn about different types of loops, such as for, while and do-while loops by
• Understanding the syntax and usage of every loop.
• Knowing when to use a particular looping construct based on the problem statement.
• Identifying potential pitfalls and errors in using looping constructs, such as infinite loops.
LEARNING OUTCOMES
At the end of this session, student will have strong understanding of how to use iterative statements to create efficient,
reliable, and maintainable C programs.
2. Whether to run the body of the loop or not depends on the provided
condition.
Type Description
for first Initializes, then it will check the condition and then
executes the body. At last, the update will be is done
either by increment or decrement.
while first Initializes, then it will check the condition, and then
executes the body. Update will be done inside the body
of loop.
do-while do-while will executes the body first and then the
condition checked.
CREATED BY K. VICTOR BABU
Session Description
For loop
• This loop is used when you know the number of times you want to repeat a block of
code. It Executes the code until the condition fails.
• The syntax of the for loop is:
while (condition) {
// code to be executed
}
1. __________ loop is used when we do not know the number of times a loop
has to be executed.
2. Another name of loop variable is________.
3. _________ is the value by which the counter variable is either increased or
decreased every time.
4. _________statement is used to assign the value to a variable.
1. int main()
{
int n;
for( n=1; n<=10; n++)
printf ("%d\t",n);
}
2. int main()
{
int n=1;
while(n <=10)
{
printf ("%d\t",n );
n++;
}
}
3. void main()
{
int n=1;
do{
printf ("%d\t",n );
n++;
} while( n >=10 );
}
CREATED BY K. VICTOR BABU
SUMMARY
This Session discussed about different types of loops available such as for,
while and do-while loops. We explored how to use these loops to execute a
block of code repeatedly, either for a specified number of times or until a
particular condition is met and also, we have done some best practices for using
loops, such as making sure to initialize loop variables correctly and avoiding
infinite loops.
2. Which for loop has range of similar indexes of 'I' used in for(I=0;I<n;I++)
(a) for(i=n;I>0;I--)
(b) for(I=N;I>=0;I--)
(c) for(I=n-1;I>0;I--)
(d) for(I=n-1;I>-1;I--)
int main()
{
int i;
for(i=0;i<5;i++);
{
printf("Hello");
}
return 0;
}
5. Rita want to print ‘n’ natural numbers by using loops. Which loop will help her to get the result fast.
Reference Books:
1. C for Engineers and Scientists – An Interpretive Approach by Harry H. Cheng, Mc Graw Hill
International Edition-2010.
2. Jeri R. Hanly, Elliot B. Koffman, “Problem Solving and Program Design in C”, 7/e, Pearson Education-
2004.
3. Let Us C: Authentic Guide to C PROGRAMMING Language 17th Edition, By Yashavant Kanetkar ·
2020