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

Similar to loops in C ppt.pdf

Similar to loops in C ppt.pdf (20)

Loop structures
Loop structuresLoop structures
Loop structures
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
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 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
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
C language 2
C language 2C language 2
C language 2
 
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
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
While loop
While loopWhile loop
While loop
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Loops
LoopsLoops
Loops
 

More from DrSamsonChepuri1 (8)

CYBER SECURITY :Cyber Law – The Legal Perspectives
CYBER SECURITY :Cyber Law – The Legal PerspectivesCYBER SECURITY :Cyber Law – The Legal Perspectives
CYBER SECURITY :Cyber Law – The Legal Perspectives
 
remoteing.ppt
remoteing.pptremoteing.ppt
remoteing.ppt
 
e-comm new2.ppt
e-comm new2.ppte-comm new2.ppt
e-comm new2.ppt
 
BASIC LAWS OF CONSUPTION AND DEMAND ANALYSIS.ppt
BASIC LAWS OF CONSUPTION AND DEMAND ANALYSIS.pptBASIC LAWS OF CONSUPTION AND DEMAND ANALYSIS.ppt
BASIC LAWS OF CONSUPTION AND DEMAND ANALYSIS.ppt
 
lect01.ppt
lect01.pptlect01.ppt
lect01.ppt
 
how-to-write-an-abstract_medical.ppt
how-to-write-an-abstract_medical.ppthow-to-write-an-abstract_medical.ppt
how-to-write-an-abstract_medical.ppt
 
research_paper_powerpoint.ppt
research_paper_powerpoint.pptresearch_paper_powerpoint.ppt
research_paper_powerpoint.ppt
 
ASN_ ppt.ppt
ASN_ ppt.pptASN_ ppt.ppt
ASN_ ppt.ppt
 

Recently uploaded

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
👉 Yavatmal Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl S...
👉 Yavatmal Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl S...👉 Yavatmal Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl S...
👉 Yavatmal Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class Call Girl S...
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 

loops in C ppt.pdf

  • 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.
  • 12. LOOPS=> FOR LOOP  Flow Diagram:
  • 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