SlideShare a Scribd company logo
2
Most read
9
Most read
15
Most read
LOOPS
PRESENTATION
LOOPS :


WHY DO WE NEED LOOPS ???

There may be a situation, when you need to
execute a block of code several number of times.
 In general statements are executed sequentially:
The first statement in a function is executed first,
followed by the second, and so on.
 A loop statement allows us to execute a statement
or group of statements multiple times

LOOPS :


TYPES OF LOOPS :

WHILE LOOP
 FOR LOOP
 DO-WHILE LOOP
 NESTED LOOP




LETS HAVE A CLOSER LOOK
LOOPS => WHILE LOOP
A while loop statement repeatedly executes a target
statement as long as a given condition is true.
Syntax:
The syntax of a while loop in C is:

while(condition)
{
statement(s);
}
LOOPS => WHILE LOOP


Here, statement(s) may be a single statement or a
block of statements. The condition may be any
expression, and true is any non-zero value. The
loop iterates while the condition is true.



When the condition becomes false, program control
passes to the line immediately following the loop
LOOPS => WHILE LOOP
FLOW DAIGRAM
LOOPS => WHILE LOOP
EXAMPLE :
#include<stdlib.h>
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
printf(“value of a:%d /n”, a);
a++;
}
getch()
}

LOOPS => WHILE LOOP


When the above code is compiled and executed, it
produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
LOOPS :
FOR LOOP:
A for loop is a repetition control structure that allows
you to efficiently write a loop that needs to execute
a specific number of times.
Syntax:
The syntax of a for loop in C is:
for ( init; condition; increment )
{
statement(s);
}
LOOPS => FOR LOOP


The init step is executed first, and only once. This
step allows you to declare and initialize any loop
control variables. You are not required to put a
statement here, as long as a semicolon appears.



Next, the condition is evaluated. If it is true, the
body of the loop is executed. If it is false, the body
of the loop does not execute and flow of control
jumps to the next statement just after the for loop.
LOOPS => FOR LOOP
After the body of the for loop executes, the flow of
control jumps back up to the increment statement.
This statement allows you to update any loop
control variables. This statement can be left
blank, as long as a semicolon appears after the
condition.
 The condition is now evaluated again. If it is
true, the loop executes and the process repeats
itself (body of loop, then increment step, and then
again condition). After the condition becomes
false, the for loop terminates.

LOOPS=> FOR LOOP


Flow Diagram:
LOOPS => FOR LOOP
Example:
#include<stdlib.h>
int main ()
{ // for loop execution
for( int a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d /n”, a);
}
getch();
}
LOOPS => FOR LOOP


When the above code is compiled and executed, it
produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
LOOPS :


DO-WHILE LOOP:



Unlike for and while loops, which test the loop
condition at the top of the loop, the do...while loop
checks its condition at the bottom of the loop.



A do...while loop is similar to a while loop, except
that a do...while loop is guaranteed to execute at
least one time.
LOOPS => DO-WHILE LOOP
Syntax:
The syntax of a do...while loop in C is:
do
{
statement(s);
}
while( condition );

Notice that the conditional expression appears at
the end of the loop, so the statement(s) in the loop
execute once before the condition is tested.
LOOPS => DO-WHILE LOOP


Flow Diagram:
LOOPS => DO-WHILE LOOP


Example:

#include<stdlib.h>
int main ()
{ // Local variable declaration:
int a = 10;
// do loop execution
do
{
printf( "value of a: %dn “ ,a);
a = a + 1;
} while( a < 20 );
getch();
}
LOOPS => DO-WHILE LOOP


When the above code is compiled and executed, it
produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
LOOPS :


NESTED LOOPS :



A loop can be nested inside of another loop.



Syntax:
The syntax for a nested for loop statement in C is as follows:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
// you can put more statements.
}
LOOPS => NESTED LOOP


EXAMPLE :

#include<stdlib.h>
int main ()
{
int a=1,b;
while(a<=3)
{
for(b=1;b<=3;b++)
{
printf("a = %d , b = %dn",a,b);
}
printf("n");
a++;
}
system("pause");
}
LOOPS => NESTED LOOP


When the above code is compiled and executed, it
produces the following result:
a=1,b=1
a=1,b=2
a=1,b=3
a=2,b=1
a=2,b=2
a=2,b=3

a=3,b=1
a=3,b=2
a=3,b=3

More Related Content

What's hot (20)

PPTX
Loops in C
Kamal Acharya
 
DOC
Jumping statements
Suneel Dogra
 
PPTX
Loops c++
Shivani Singh
 
PPTX
Looping statements in C
Jeya Lakshmi
 
PPTX
Forloop
Dipen Vasoya
 
PPTX
types of loops and what is loop
waheed dogar
 
PPTX
Loops in c language
tanmaymodi4
 
PDF
10. switch case
Way2itech
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Types of loops in c language
sneha2494
 
PPTX
Loops in C Programming Language
Mahantesh Devoor
 
PPT
Control structure
Samsil Arefin
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PPTX
While loop,Do While loop ,for loop
MuhammadWaseem305
 
PPT
Looping in C
Prabhu Govind
 
PPTX
Switch Case in C Programming
Sonya Akter Rupa
 
PPTX
Python Flow Control
Kamal Acharya
 
PPTX
Control statements in c
Sathish Narayanan
 
Loops in C
Kamal Acharya
 
Jumping statements
Suneel Dogra
 
Loops c++
Shivani Singh
 
Looping statements in C
Jeya Lakshmi
 
Forloop
Dipen Vasoya
 
types of loops and what is loop
waheed dogar
 
Loops in c language
tanmaymodi4
 
10. switch case
Way2itech
 
C Programming: Control Structure
Sokngim Sa
 
Control Statements in Java
Niloy Saha
 
Types of loops in c language
sneha2494
 
Loops in C Programming Language
Mahantesh Devoor
 
Control structure
Samsil Arefin
 
Function C programming
Appili Vamsi Krishna
 
While loop,Do While loop ,for loop
MuhammadWaseem305
 
Looping in C
Prabhu Govind
 
Switch Case in C Programming
Sonya Akter Rupa
 
Python Flow Control
Kamal Acharya
 
Control statements in c
Sathish Narayanan
 

Similar to Loops Basics (20)

PDF
loops in C ppt.pdf
DrSamsonChepuri1
 
PDF
Loop and while Loop
JayBhavsar68
 
PPTX
Cse lecture-7-c loop
FarshidKhan
 
DOCX
Looping statements
Chukka Nikhil Chakravarthy
 
PDF
cpu.pdf
RAJCHATTERJEE24
 
DOCX
loops and iteration.docx
JavvajiVenkat
 
PPTX
C Programming: Looping Statements in C Pgm
Navya Francis
 
PPTX
The Loops
Krishma Parekh
 
PPTX
Decision Making and Looping
Munazza-Mah-Jabeen
 
DOCX
itretion.docx
JavvajiVenkat
 
PDF
3. Flow Controls in C (Part II).pdf
santosh147365
 
PDF
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
 
PPTX
Loops
SAMYAKKHADSE
 
PPTX
Loop (Computer programming and utilization)
Digvijaysinh Gohil
 
PPTX
Presentation on nesting of loops
bsdeol28
 
DOCX
Programming Fundamentals lecture 8
REHAN IJAZ
 
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
PPTX
Loop in C Properties & Applications
Emroz Sardar
 
PPTX
Loops in c
shubhampandav3
 
PPTX
C Programming Language Part 6
Rumman Ansari
 
loops in C ppt.pdf
DrSamsonChepuri1
 
Loop and while Loop
JayBhavsar68
 
Cse lecture-7-c loop
FarshidKhan
 
Looping statements
Chukka Nikhil Chakravarthy
 
loops and iteration.docx
JavvajiVenkat
 
C Programming: Looping Statements in C Pgm
Navya Francis
 
The Loops
Krishma Parekh
 
Decision Making and Looping
Munazza-Mah-Jabeen
 
itretion.docx
JavvajiVenkat
 
3. Flow Controls in C (Part II).pdf
santosh147365
 
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
 
Loop (Computer programming and utilization)
Digvijaysinh Gohil
 
Presentation on nesting of loops
bsdeol28
 
Programming Fundamentals lecture 8
REHAN IJAZ
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
Loop in C Properties & Applications
Emroz Sardar
 
Loops in c
shubhampandav3
 
C Programming Language Part 6
Rumman Ansari
 
Ad

Recently uploaded (20)

PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Ad

Loops Basics

  • 2. LOOPS :  WHY DO WE NEED LOOPS ??? There may be a situation, when you need to execute a block of code several number of times.  In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.  A loop statement allows us to execute a statement or group of statements multiple times 
  • 3. LOOPS :  TYPES OF LOOPS : WHILE LOOP  FOR LOOP  DO-WHILE LOOP  NESTED LOOP   LETS HAVE A CLOSER LOOK
  • 4. LOOPS => WHILE LOOP A while loop statement repeatedly executes a target statement as long as a given condition is true. Syntax: The syntax of a while loop in C is: while(condition) { statement(s); }
  • 5. LOOPS => WHILE LOOP  Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.  When the condition becomes false, program control passes to the line immediately following the loop
  • 6. LOOPS => WHILE LOOP FLOW DAIGRAM
  • 7. LOOPS => WHILE LOOP EXAMPLE : #include<stdlib.h> int main () { // Local variable declaration: int a = 10; // while loop execution while( a < 20 ) { printf(“value of a:%d /n”, a); a++; } getch() } 
  • 8. LOOPS => WHILE LOOP  When the above code is compiled and executed, it produces the following result: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 9. LOOPS : FOR LOOP: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax: The syntax of a for loop in C is: for ( init; condition; increment ) { statement(s); }
  • 10. LOOPS => FOR LOOP  The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.  Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
  • 11. LOOPS => FOR LOOP After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.  The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates. 
  • 13. LOOPS => FOR LOOP Example: #include<stdlib.h> int main () { // for loop execution for( int a = 10; a < 20; a = a + 1 ) { printf("value of a: %d /n”, a); } getch(); }
  • 14. LOOPS => FOR LOOP  When the above code is compiled and executed, it produces the following result: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 15. LOOPS :  DO-WHILE LOOP:  Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop.  A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
  • 16. LOOPS => DO-WHILE LOOP Syntax: The syntax of a do...while loop in C is: do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.
  • 17. LOOPS => DO-WHILE LOOP  Flow Diagram:
  • 18. LOOPS => DO-WHILE LOOP  Example: #include<stdlib.h> int main () { // Local variable declaration: int a = 10; // do loop execution do { printf( "value of a: %dn “ ,a); a = a + 1; } while( a < 20 ); getch(); }
  • 19. LOOPS => DO-WHILE LOOP  When the above code is compiled and executed, it produces the following result: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 20. LOOPS :  NESTED LOOPS :  A loop can be nested inside of another loop.  Syntax: The syntax for a nested for loop statement in C is as follows: for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); // you can put more statements. }
  • 21. LOOPS => NESTED LOOP  EXAMPLE : #include<stdlib.h> int main () { int a=1,b; while(a<=3) { for(b=1;b<=3;b++) { printf("a = %d , b = %dn",a,b); } printf("n"); a++; } system("pause"); }
  • 22. LOOPS => NESTED LOOP  When the above code is compiled and executed, it produces the following result: a=1,b=1 a=1,b=2 a=1,b=3 a=2,b=1 a=2,b=2 a=2,b=3 a=3,b=1 a=3,b=2 a=3,b=3