Different types of loops-Prasad.ppt

DxTechGaming 11 views 11 slides Jan 06, 2023
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

loops in c


Slide Content

Chapter 6: Loops

Introduction
Aloopis a sequence of instruction s that is continually
repeated until a certain condition is reached.

Loops type in c

The While statement
Awhileloop in C programming repeatedly executes a target
statement as long as a given condition is true.
Syntax: while(condition)
{
statement(s);
}
Statement(s)may be a single statement or a block of
statements. Theconditionmay be any expression, and true
is any nonzero value. The loop iterates while the condition is
true.
When the condition becomes false, the program control
passes to the line immediately following the loop.

While: Flow Diagram

Infinite Loop Using while
While loop won’t terminate if the controlling expression
always has a nonzero value
Eg: while(1)
Will execute forever unless its body contains a statement
that transfers control out of loop (break, goto, return)

The do… while statement
do.... While is essentially just a while statement whose
controlling expression is tested after each execution of
the loop body
Syntax
do{
statements
}
while (expression);

for statement
Aforloop is a repetition control structure that allows you
to efficiently write a loop that needs to execute a specific
number of times.
Syntax
for(init; condition; increment/decrement
{
statements;
}

Infinite Loop using for
Like while infinite loop can be created using for
Syntax(or code): for(;;)

Exiting from loop
The break statement
Example: Program to check
whether a number
is prime or not
Tags