0% found this document useful (0 votes)
2 views

Iterative Statements

This session focuses on iterative statements in C programming, specifically for, while, and do-while loops, aiming to teach their syntax, usage, and potential pitfalls. Students will learn to effectively use these loops to automate repetitive tasks and enhance code efficiency. The session includes examples, self-assessment questions, and references for further learning.

Uploaded by

sivaprasadsoft
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Iterative Statements

This session focuses on iterative statements in C programming, specifically for, while, and do-while loops, aiming to teach their syntax, usage, and potential pitfalls. Students will learn to effectively use these loops to automate repetitive tasks and enhance code efficiency. The session includes examples, self-assessment questions, and references for further learning.

Uploaded by

sivaprasadsoft
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Department of BES-1

COMPUTATIONAL THINKING FOR


STRUCTURED DESIGN

Topic:

ITERATIVE STATEMENTS
Session -

CREATED BY K. VICTOR BABU


AIM OF THE SESSION

The aim of a session is to familiarise the concepts related to loops or iterations.

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.

CREATED BY K. VICTOR BABU


INTRODUCTION

In this session, we will discuss about iterative statements, also known as


loops, which allows programmer to execute a set of statements repeatedly
based on some specific conditions. There are three types of iterative
statements:

• for loop: It allows programmer to execute a set of statement or


statements repeatedly, based on a loop variable condition.
• while loop: It allows programmer to repeat a statement or set of
statements as long as some condition is satisfied.
• do-while loop: It allows programmer to execute set of statement or
statements at least once before the condition is checked.

These iterative statements are important in C programming as they help to


automate repetitive tasks and make the code more efficient and makes it
easier to handle complex logics.

CREATED BY K. VICTOR BABU


Session Description
According to the position of a control statement there are two forms of looping statements
which are categorised as:
1.Entry-controlled loop: Before executing body of the loop, condition is verified. It is also
known as pre-checking loop.
2.Exit controlled loop: After executing body of the loop condition is verified. It is also
known aa post-checking loop.

An infinite loop has the following characteristics:

1. Termination condition are specified.

2. Whether to run the body of the loop or not depends on the provided
condition.

Figure -1: Loops flow Diagram

CREATED BY K. VICTOR BABU


Session Description
There are 3 different loop constructs, they
are:
1.For loop
2.While loop
3.Do-while loop

​ ​
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:

for (initialization; condition; increment/decrement)


{
// code to be executed
}

• Here, initialization is the initial value of the loop control variable,


• condition is the condition for continuing the loop,
• and increment/decrement is the value by which the loop control variable is
changed after each iteration.

CREATED BY K. VICTOR BABU


Session Description
While loop
• This loop is used when you don't know the number of times you want
to repeat a block of code but have a condition that needs to be met
before each iteration.
• The syntax of the while loop is:

while (condition) {
// code to be executed
}

CREATED BY K. VICTOR BABU


Session Description
Do-While loop
• This loop is similar to the while loop, but the block of code is executed
at least once before the condition is checked.
• The syntax of the do-while loop is:
do {
// code to be executed
} while (condition);

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

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.

CREATED BY K. VICTOR BABU


EXAMPLES

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.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. Which of the following is an exit-controlled loop?

(a) While loop


(b) For loop
(c) do-while loop
(d) None of the Above

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--)

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

3. The output of this c Code is?


void main()
{
int x=0;
for(x<3;x++)
{
printf("Hello");
}
}

(a) Compile time error


(b) Hello is printed thrice
(c) Nothing
(d) Varies

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

1. Explain the significance of the loops?


2. What is the difference between Exit-controlled loop and Entry-controlled loop.
3. Find the output of the following snippet?
int main()
{
for(;;)
printf("Hello..");
return 0;
}

4. Find the output of the following?

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.

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

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

Sites and Web links:


1. https://www.guru99.com/c-loop-statement.html#1
2. https://www.geeksforgeeks.org/c-loops/

CREATED BY K. VICTOR BABU


THANK YOU

CREATED BY K. VICTOR BABU

You might also like