Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

• Know the loop

• Types of loop statements in C


1. While Loop
2. do-while Loop
3. For Loop
4. Nested Loop
Loop is a statement or a set
of statements that is
executed repeatedly.
Advantages of Loop
 To execute a statement or a number of
statement for a specified number of
times.
 To use a sequence of values
While Loop

 While Loop executes one or


more statement while the
given condition is true.
Syntax
While (condition)
{
Statement 1;
Statement 2;
.
.
Statement N;
}
Working of While Loop
A while statement has the following syntax:
 If the condition is true, the statement is executed
 Then the condition is evaluated again, and if it is
still true, the statement is executed again
 The statement is executed repeatedly until the
condition becomes false
Logic of a while Loop
condition

true false

statement
Write a program that display counting from 1
to 1 10 using loop while loop

#include<stdio.h>
#include<conio.h>
int main()
{
Int n;
n=1;
while(n<=10)
{
printf("%d\n",n);
n++;
}
getch();
}
DO-WHILE LOOP
Q: What is do-while Loo?

 Do-while loop execute one or more statements while


the given condition is true.
 The important point in this loop is, the condition
comes after the body of the loop.
 In this loop, each statement must be executed at least
once.
 Semicolon is used at the end of condition.
Syntax:

Do
{
Statement 1;
Statement 2;
.
.
Statement N;
}
While (condition);
Syntax Explanation

Do: It is a keyword that indicate the beginning of the loop.


Condition: The loop is continues only the given condition is
true. When the condition is false the loop is terminated.
Statement: Statement is the instruction that is executed when the
condition is true. Two or more statements are written in braces { }.
It is the body of the loop.
Flow Chart:

Loop Body

T T
Conditio
n

F
Difference of While & Do-While Loop

While loop Do-while Loop


1) In this loop, condition comes before the 1) In this loop, condition comes
the body of the loop. After the body of the loop.
2) If the condition is false in the beginning 2) If the condition is false in the
While loop is never executed. Beginning the loop is executed at
3) Semicolon is not used at the end of least once.
Condition. 3) Semicolon is used at the end of
statement.
Write a program print first ten natural no. on
the computer screen by using do while loop
#include (stdio.h)
#include (conio.h)
Void main ()
{
Int n;
n=1;
do
{
Printf (“%d\n”,c);
n++;
}
While (n<=10);
getch ();
}
For Loop
What is for loop

For loop execute one or more statement for


a special number of time. This loop also
called counter controlled loop. It is most
flexible loop.
Syntax
For (Initialization; Condition; increment/decrement)
{
Statement 1;
Statement 2;
.
.
Statement N;
}
Working of for loop
 The statement is execution only is given condition
is true. If the condition is false the statement is
never execution
 The part of loop specifies the changes in counter
variable after each execution of the loop.
 Statement is execution when the condition is true If
two or more statement are used, these are given in
brace {}.
Flow chart
Initialization

Increment /
Decrement

Condition
T Loop Body

F
Write a program that display counting
from 1 to 5 using for loop
#Include <stdio.h>
#Include <conio.h>
void main ()
{
{ Int n;
n=1;
for (n=1, n<5, n++)
printf (“%d\n”,n);
getch ();
}
Nested Loop
Loop with in a loop is called nested loop
Outer Loop
Syntax
For (initialization; condition; increment/decrement )
{
For (initialization; condition; increment/decrement )
{
Inner Loop Statement ();
}

}
Flowchart
Types of nested loop

 Nested while loop


 Nested do-while loop
 Nested for loop
Nested While loop
A while loop inside another while loop is called nested while loop.

Syntax of Nested while loop

while (condition1) {
 statement (s);  
while (condition2)

statement(s);  
... ... ...
}
  ... ... ...
}
Nested do-while loop
A do-while loop inside another do-while loop is called nested do-while loop

Syntax of Nested do-while loop


do

statement(s);  
do

statement(s);  
... ... ...
}
while (condition2);
... ... ...
}
while (condition1);
Nested Do-While-loop:
For example:
Do
{
// body of outer while loop do
{
// body of inner while loop

} while (condition-2);
// body of outer while loop

} while (condition-1);
Nested for loop
A for loop inside another for loop is called nested for loop.

Syntax of Nested for loop


for (initialization; condition; increment/decrement)

statement(s);  

for (initialization; condition; increment/decrement)


{
  statement(s);  
... ... ...

... ... ...
}
Nested for loop
for (int i=0; i<5; i++)
{
// body of outer for loop
for (int j=0; j<5; j++)
{
// body of inner for loop
}
// body of outer for loop
}
Nested while loop

while (condition-1)
{
// body of outer while loop
while (condition-2)
{
// body of inner while loop
}
// body of outer while loop
}

You might also like