You are on page 1of 12

Loops in C Programming

-----------------------------------
---------
Presentation on Loop
By:-
SUKRITI RAI
(B.Sc. 1st year )
UNDER THE SUPERVISION OF
Er. Sachin Jaiswal
Introduction to Loops
* A loop is defined as a block of
statements, which are repeatedly executed
for a certain number of times
* A loop executes the sequence of statement
many times until the stated condition
becomes false.
A loop consists of two parts , a body of a
loop and a control statement
Types of Loops
In Pre – Test Loop, condition is checked before
the beginning of each iteration. If condition is
TRUE repetition is performed, if it is FALSE
repetition is not performed

“Pre – Test loop is implemented using


‘while’ statement”
* In Post – Test Loop, first the block of statements
to be repeat is executed then condition will be
tested
*That means in Post – Test Loop, condition is
checked after executing the repeating
statements, if the condition is TRUE it repeat the
statements again, if it is FALSE repetition is not
performed

“Post – Test loop is implemented using ‘do -


while’ statement”
FOR LOOP -:
---------------
syntax
for(initialization ; condition ; increment/decrement)
{
statement();
}

The initialization expression set the initial value of the loop control variable.

The condition test the value of the loop control variable.

The increment or decrement update the loop control variable.


Program of For Loop :-
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=5;i++)
{
printf(“Hello\n”);
}
getch();
}
Output of the above program
Hello
Hello
Hello
Hello
Hello
While Loop:-
Syntax
While (condition)
{
statement();
}

In the while loop , when the condition is true , then the while loop’s statement will
be executed , otherwise not.
The statement is repeated as long as the repetition condition is true
Comparing For and While Loop:---
Program of While Loop:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
while(i<=5)
{
printf(“%d \n” , i);
i++;
}
getch();
}

Output of the above program------


1
2
3
4
5
Reference:-
[1]. E Balaguruswamy, “Programming in ANSI C”, Third edition,

[2].Google.com

[3] PDF notes provided by Sachin Sir …………………….


Thank You !
Have a Nice day……

You might also like