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

Pps Class 10

The document provides an overview of loops in the C programming language, explaining their purpose and structure. It details three types of loops: while loop, for loop, and do while loop, including their syntax and execution flow. Each loop type is illustrated with example code to demonstrate its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Pps Class 10

The document provides an overview of loops in the C programming language, explaining their purpose and structure. It details three types of loops: while loop, for loop, and do while loop, including their syntax and execution flow. Each loop type is illustrated with example code to demonstrate its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

by

Mr.Bhavani Sankar Panda

Course on
Programming for Problem
Solving
: Loops
LOOPS IN C
• In any programming language including C, loops are used to
execute a set of statements repeatedly until a particular condition
is satisfied.
if the Test Condition is true, then the loop is
executed, and if it is false then the execution
breaks out of the loop. After the loop is
successfully executed the execution again starts
from the Loop entry and again checks for the Test
condition, and this keeps on repeating.

The sequence of statements to be executed is kept


inside the curly braces { } known as the Loop
body. After every execution of the loop body,
condition is verified, and if it is found to be true the
loop body is executed again. When the condition
check returns false, the loop body is not executed,
and execution breaks out of the loop.
TYPES OF LOOP

• There are 3 types of Loop in C language, namely:

1.while loop
2.for loop
3.do while loop
WHILE LOOP
• while loop can be addressed as variable initialization;
while(condition)
an entry control loop. It is
{
completed in 3 steps. statements;
variable increment or decrement;
}
• Variable initialization.(e.g int x =
#include<stdio.h>
0;) void main( )
• condition(e.g while(x <= 10)) {
int x;
• Variable increment or decrement x = 1;
( x++ or x-- or x = x + 2 ) while(x <= 10)
{
printf("%d\t", x);
x++;
}
}
FOR LOOP
• for loop is used to execute a set of statements repeatedly until a
particular condition is satisfied. We can say it is an open ended
loop.. General format is,
for(initialization; condition; increment/decrement)
{
statement-block;
}
In for loop we have exactly two semicolons, one after initialization and second after the
condition. In this loop we can have more than one initialization or increment/decrement,
separated using comma operator. But it can have only one condition.

The for loop is executed as follows:

1. It first evaluates the initialization code.


2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and again follows from step 2.
5. When the condition expression becomes false, it exits the loop.
#include<stdio.h>

void main( )
{
int x;
for(x = 1; x <= 10; x++)
{
printf("%d\t", x);
}
}
DO WHILE LOOP
• In some situations it is necessary to execute body of the loop before testing
the condition. Such situations can be handled with the help of do-while loop.
do statement evaluates the body of the loop first and at the end, the condition
is checked using while statement. It means that the body of the loop will be
executed at least once, even though the starting condition inside while is
initialized to be false. General syntax is,
#include<stdio.h>
do
{ void main()
{
.....
int a, i;
..... a = 5;
} i = 1;
do
while(condition);
{
printf("%d\t", a*i);
i++;
}
while(i <= 10);
}
Thank You

You might also like