SlideShare a Scribd company logo
1 of 22
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

Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

What's hot (20)

Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...while
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Looping in C
Looping in CLooping in C
Looping in C
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
10. switch case
10. switch case10. switch case
10. switch case
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Control statements
Control statementsControl statements
Control statements
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Loops
LoopsLoops
Loops
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 

Viewers also liked (6)

Function in c
Function in cFunction in c
Function in c
 
Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
String c
String cString c
String c
 
String in c
String in cString in c
String in c
 

Similar to Loops Basics

Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
Deepak Singh
 

Similar to Loops Basics (20)

loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
C language 2
C language 2C language 2
C language 2
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Deeksha gopaliya
Deeksha gopaliyaDeeksha gopaliya
Deeksha gopaliya
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
Loop
LoopLoop
Loop
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

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