Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views30 pages

For Loops

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

For Loops

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

Loops in C

Looping statement defines a set of repetitive statements .


These statements are repeated, with same or different
parameters for a number of times.
In programming, there exists situations when you need to
repeat single or a group of statements till some condition is
met. Such as - read all files of a directory.
Looping statements are also known as iterative or repetitive
statement.
There are three types of looping statement in C.
for loop
while loop
do...while loop
1
when we need to perform a set of task repeatedly till
some condition is met.
Such as – sending email to all employees, deleting all files,
printing 1000 pages of a document. All of these tasks are
performed in loop.
To do such task C supports looping control statements.
For loop is an entry controlled looping statement. It is
used to repeat set of statements until some condition is
met.

Looping statements whose condition is checked prior to the execution of its


body is called as Entry controlled loop.

2
Syntax;

for(variable-initialization ; condition ;
variable-update)
{
// Body of for loop
}

Syntax:

3
while:

while loop is an entry controlled looping construct.


We use while loop to repeat set of statements when
number of iterations are not known prior to its
execution. It provides flexibility to define loop
without initialization and update parts (present in
for loop).
Syntax:

while(condition)
{
// Body of while loop
} 4
main()
{
/* Loop counter variable declaration and initialization*/
int n = 1;

/* Loop condition */
while(n <= 10)
{
/* Body of loop */
printf("%d ", n);

/* Update loop counter variable */


n++;
}

}
Output -
1 2 3 4 5 6 7 8 9 10

5
6
7
do while:

do...while is an exit controlled looping statement. We use


do...while loop when there is a need to check condition after
execution of loop body. do...while loop in any case executes
minimum once.

Looping statements whose condition is checked after execution


of its loop body is called as Exit controlled loop
Syntax:

do
{
// Body of do while loop
} while (condition); 8
#include <stdio.h>

main()
{
/* Loop counter variable declaration */
int n=1;

do
{
/* Body of loop */
printf("%d ", n);

/* Update loop counter variable */


n++;

} while(n <= 10); /* Loop condition */

}
Output -
1 2 3 4 5 6 7 8 9 10
12
13
14
15
16
17
18
19
20
21
22
Computer Science: A Structured Programming Approach Using C 23
Computer Science: A Structured Programming Approach Using C 24
Computer Science: A Structured Programming Approach Using C 25
Computer Science: A Structured Programming Approach Using C 26
27
28
29
30

You might also like