SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Gagan Deep 
Rozy Computech Services 
3rd Gate, K.U., Kurukshetra-136119 
M- 9416011599 
Email – rozygag@yahoo.com
Looping 
Suppose we want to display hello on output screen five 
times in five different lines. 
We might think of writing either five printf statements or 
one printf statement consisting of constant “hellon” five 
times. 
What if we want to display hello 500 times? 
Should we write 500 printf statement or equivalent ? 
Obviously not. 
It means that we need some programming facility to 
repeat certain works. 
Such facility in Programming Languages is available in 
the form Looping Statements.
Looping 
Loops are used to repeat something. 
Repetition of block of code. 
If program requires that group of instructions be executed 
repeatedly is known as looping. 
Looping is of two types 
Conditional 
Unconditional 
Conditional Looping: Computation continues indefinitely 
until the logical condition is true. 
Unconditional Looping : The number of repetition known 
in advance or repetition is infinite.
Types of Loops 
Conditional Loops 
While Loop – Pre Condition Loop – Entry Time 
Conditional Loop 
Do-While Loop – Post Condition Loop – Exit Time 
Conditional Loop 
Unconditional Loop 
For Loop – Counted Loops 
Uncounted Loop – Infinite loop 
Uncounted Loop –infinite loop controlled by 
control statements.
while Statement 
The while statement is used to carry out looping 
operations, in which a group of statement is executed 
repeatedly, until some condition has been satisfied. 
The general form of statement is 
while (test expression) 
statement to be executed; 
or 
while (test expression) 
{ statements to be executed }
Program 1-4 First 10 Natural Numbers 
#include <stdio.h> 
void main() 
{ int i=0; 
while(i<=9) // i<10 
printf(“%d”, i++); 
}/*here we can’t use ++i – that gives 
different result*/ 
//only a statement 
#include <stdio.h> 
void main() 
{ int i=0; 
while(i<=9) // i<10 
{ printf(“%dn”, i); 
i++; }//compound statement 
} //we also can use ++i 
#include <stdio.h> 
void main() 
{ int i=10; 
while(i<=9) // i<10 
{ printf(“%d”, i); 
i++; } 
// doesn’t work body of loop 
} 
#include <stdio.h> 
void main() 
{ int i=9; 
while(i>=0) // i>-1 
{ printf(“%d”, i); 
i--; } //--i 
}//decrement – decreasing order
do…..while Statement 
When a loop is constructed using the while 
statement, the test for continuation of the loop 
carried out at the beginning of each pass. 
Sometimes, however it is desirable to have a loop with 
the test for continuation at the end the end of each 
pass. This can be accomplished by do..while 
statement. The general form of statement is 
do Statement; //single statement 
while (expression); or 
do 
{ statements to be executed } 
while (expression);
Program 5-8 First 10 Natural Numbers 
#include <stdio.h> 
void main() 
{ int i=0; 
do 
printf(“%d”, i++); 
while(i<=9); } 
#include <stdio.h> 
void main() 
{ int i=0; 
do 
{printf(“%dn”, i); 
i++; } 
while(i<=9) ; } 
#include <stdio.h> 
void main() 
{ int i=10; 
do //first execute then check 
{ printf(“%d”, i); 
i++; } while(i<=9) ; } 
// prints 10(execute one time) 
#include <stdio.h> 
void main() 
{ int i=9; 
do{ printf(“%d”, i); 
i--; } 
while(i>=0) ; }
for Statement 
The for statement is the third and perhaps the most 
commonly used looping statement in C. 
This statement includes an expression 
that specifies an initial value for an index, 
another expression 
that determines whether or not the loop is continued, 
and a third expression 
that allows the index to be modified at the end of 
each pass. 
The general form of statement is 
for (expr1; expr2; expr3) 
statement; or { statements }
Print 1 to 10 Program 9-12 
#include<stdio.h> 
#include<stdio.h> 
void main() 
void main() 
{int i; 
{ 
for(i=1;i<=10;i++) 
for(int i=1;i<=10;++i) 
printf(“%d”,i); } 
printf(“%d”,i); } 
#include<stdio.h> 
void main() 
{ 
for(int i=1;i<=10;i=i+1) 
printf(“%d”,i); 
} 
#include<stdio.h> 
void main() 
{int i=1; 
for( ; i<=10; ) 
{ printf(“%d”,i); 
i++ ; } 
}//you can also use ++i
Program 13-16 
#include<stdio.h> 
void main() 
{for(int i=0;i<5;i++) 
{ } 
} // i goes to 5 only 
#include<stdio.h> 
void main() 
{ 
for(i=0;i<5;i++); 
} // i goes to 5 only 
Comma Operator 
#include<stdio.h> 
void main() 
{ 
for(i=0,j=0;i<5;i++, j++) { 
statement1; statement2; 
statement3; } 
#include<stdio.h> 
void main() 
{int i = 0; 
for( ; ;) 
{ statements; 
if(breaking condition) 
break; i++; }
Program 17-18 
Reverse Number 
#include<stdio.h> 
void main() 
{n, r, rn=0; 
scanf(“%d”, &n); 
while (n!=0) { 
r=n%10; n=n/10 ; 
rn=rn*10+r; } 
printf(“Reverese Number 
=%d”, rn); 
} 
Palindrome Number 
#include<stdio.h> 
void main() 
{n, r, T, rn=0; 
scanf(“%d”, &n); T=n; 
while (T!=0) { 
r=T%10; T=T/10 ; 
rn=rn*10+r; } 
if (rn==n) 
printf(“Palindrome Number); 
else 
printf(“Non-Palindrome 
Number); 
}
Program 19 
Prime Number 
#include<stdio.h> 
void main() 
{n, i; 
scanf(“%d”, &n); 
while (n % i != 0) 
i++; 
if (n==i) 
printf(“%d is prime”, n); 
}
If you have any queries you can 
contact me at : 
rozygag@yahoo.com

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
File Management in C
File Management in CFile Management in C
File Management in C
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Python | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python TutorialPython | What is Python | History of Python | Python Tutorial
Python | What is Python | History of Python | Python Tutorial
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
C Basics
C BasicsC Basics
C Basics
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
Python programming
Python  programmingPython  programming
Python programming
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
C++ goto statement tutorialspoint
C++ goto statement   tutorialspointC++ goto statement   tutorialspoint
C++ goto statement tutorialspoint
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
Loops in C
Loops in CLoops in C
Loops in C
 
Loops in c
Loops in cLoops in c
Loops in c
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 

Andere mochten auch

Steps to migrate vb6 application to vb dotnet
Steps to migrate vb6 application to vb dotnetSteps to migrate vb6 application to vb dotnet
Steps to migrate vb6 application to vb dotnetDebabrata Adhya
 
.Net branching and flow control
.Net branching and flow control.Net branching and flow control
.Net branching and flow controlLearnNowOnline
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedurepragya ratan
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statementspragya ratan
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
visual basic for the beginner
visual basic for the beginnervisual basic for the beginner
visual basic for the beginnerSalim M
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.netilakkiya
 
Looping statement in vb.net
Looping statement in vb.netLooping statement in vb.net
Looping statement in vb.netilakkiya
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computersimran153
 

Andere mochten auch (16)

Introduction to Visual Basic (Week 2)
Introduction to Visual Basic (Week 2)Introduction to Visual Basic (Week 2)
Introduction to Visual Basic (Week 2)
 
Vbasic
VbasicVbasic
Vbasic
 
Steps to migrate vb6 application to vb dotnet
Steps to migrate vb6 application to vb dotnetSteps to migrate vb6 application to vb dotnet
Steps to migrate vb6 application to vb dotnet
 
.Net branching and flow control
.Net branching and flow control.Net branching and flow control
.Net branching and flow control
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
C++ loop
C++ loop C++ loop
C++ loop
 
visual basic for the beginner
visual basic for the beginnervisual basic for the beginner
visual basic for the beginner
 
Loops c++
Loops c++Loops c++
Loops c++
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
 
Looping statement in vb.net
Looping statement in vb.netLooping statement in vb.net
Looping statement in vb.net
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Control statements
Control statementsControl statements
Control statements
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computer
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Ähnlich wie Loops in C - Types, Statements and Examples

Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareGagan Deep
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfDrDineshenScientist
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Computer programming
Computer programmingComputer programming
Computer programmingXhyna Delfin
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguageTanmay Modi
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin cVikash Dhal
 

Ähnlich wie Loops in C - Types, Statements and Examples (20)

Programming basics
Programming basicsProgramming basics
Programming basics
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
3. control statement
3. control statement3. control statement
3. control statement
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
What is c
What is cWhat is c
What is c
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Computer programming
Computer programmingComputer programming
Computer programming
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 

Mehr von Gagan Deep

Fundamentals of Neural Networks
Fundamentals of Neural NetworksFundamentals of Neural Networks
Fundamentals of Neural NetworksGagan Deep
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial IntelligenceGagan Deep
 
Software Project Planning V
Software Project Planning VSoftware Project Planning V
Software Project Planning VGagan Deep
 
Software Project Planning IV
Software Project Planning IVSoftware Project Planning IV
Software Project Planning IVGagan Deep
 
Software Project Planning III
Software Project Planning IIISoftware Project Planning III
Software Project Planning IIIGagan Deep
 
Software Project Planning II
Software Project Planning IISoftware Project Planning II
Software Project Planning IIGagan Deep
 
Software Project Planning 1
Software Project Planning 1Software Project Planning 1
Software Project Planning 1Gagan Deep
 
Software Engineering
Software Engineering Software Engineering
Software Engineering Gagan Deep
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
C – A Programming Language- I
C – A Programming Language- IC – A Programming Language- I
C – A Programming Language- IGagan Deep
 
System Analysis & Design - 2
System Analysis & Design - 2System Analysis & Design - 2
System Analysis & Design - 2Gagan Deep
 
System Analysis & Design - I
System Analysis & Design - ISystem Analysis & Design - I
System Analysis & Design - IGagan Deep
 
Information System and MIS
Information System and MISInformation System and MIS
Information System and MISGagan Deep
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial IGagan Deep
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebraGagan Deep
 
Normalization 1
Normalization 1Normalization 1
Normalization 1Gagan Deep
 
Normalization i i
Normalization   i iNormalization   i i
Normalization i iGagan Deep
 
Plsql overview
Plsql overviewPlsql overview
Plsql overviewGagan Deep
 

Mehr von Gagan Deep (19)

Number system
Number systemNumber system
Number system
 
Fundamentals of Neural Networks
Fundamentals of Neural NetworksFundamentals of Neural Networks
Fundamentals of Neural Networks
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial Intelligence
 
Software Project Planning V
Software Project Planning VSoftware Project Planning V
Software Project Planning V
 
Software Project Planning IV
Software Project Planning IVSoftware Project Planning IV
Software Project Planning IV
 
Software Project Planning III
Software Project Planning IIISoftware Project Planning III
Software Project Planning III
 
Software Project Planning II
Software Project Planning IISoftware Project Planning II
Software Project Planning II
 
Software Project Planning 1
Software Project Planning 1Software Project Planning 1
Software Project Planning 1
 
Software Engineering
Software Engineering Software Engineering
Software Engineering
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
C – A Programming Language- I
C – A Programming Language- IC – A Programming Language- I
C – A Programming Language- I
 
System Analysis & Design - 2
System Analysis & Design - 2System Analysis & Design - 2
System Analysis & Design - 2
 
System Analysis & Design - I
System Analysis & Design - ISystem Analysis & Design - I
System Analysis & Design - I
 
Information System and MIS
Information System and MISInformation System and MIS
Information System and MIS
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial I
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Normalization 1
Normalization 1Normalization 1
Normalization 1
 
Normalization i i
Normalization   i iNormalization   i i
Normalization i i
 
Plsql overview
Plsql overviewPlsql overview
Plsql overview
 

Kürzlich hochgeladen

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Kürzlich hochgeladen (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Loops in C - Types, Statements and Examples

  • 1. Gagan Deep Rozy Computech Services 3rd Gate, K.U., Kurukshetra-136119 M- 9416011599 Email – rozygag@yahoo.com
  • 2. Looping Suppose we want to display hello on output screen five times in five different lines. We might think of writing either five printf statements or one printf statement consisting of constant “hellon” five times. What if we want to display hello 500 times? Should we write 500 printf statement or equivalent ? Obviously not. It means that we need some programming facility to repeat certain works. Such facility in Programming Languages is available in the form Looping Statements.
  • 3. Looping Loops are used to repeat something. Repetition of block of code. If program requires that group of instructions be executed repeatedly is known as looping. Looping is of two types Conditional Unconditional Conditional Looping: Computation continues indefinitely until the logical condition is true. Unconditional Looping : The number of repetition known in advance or repetition is infinite.
  • 4.
  • 5. Types of Loops Conditional Loops While Loop – Pre Condition Loop – Entry Time Conditional Loop Do-While Loop – Post Condition Loop – Exit Time Conditional Loop Unconditional Loop For Loop – Counted Loops Uncounted Loop – Infinite loop Uncounted Loop –infinite loop controlled by control statements.
  • 6. while Statement The while statement is used to carry out looping operations, in which a group of statement is executed repeatedly, until some condition has been satisfied. The general form of statement is while (test expression) statement to be executed; or while (test expression) { statements to be executed }
  • 7. Program 1-4 First 10 Natural Numbers #include <stdio.h> void main() { int i=0; while(i<=9) // i<10 printf(“%d”, i++); }/*here we can’t use ++i – that gives different result*/ //only a statement #include <stdio.h> void main() { int i=0; while(i<=9) // i<10 { printf(“%dn”, i); i++; }//compound statement } //we also can use ++i #include <stdio.h> void main() { int i=10; while(i<=9) // i<10 { printf(“%d”, i); i++; } // doesn’t work body of loop } #include <stdio.h> void main() { int i=9; while(i>=0) // i>-1 { printf(“%d”, i); i--; } //--i }//decrement – decreasing order
  • 8. do…..while Statement When a loop is constructed using the while statement, the test for continuation of the loop carried out at the beginning of each pass. Sometimes, however it is desirable to have a loop with the test for continuation at the end the end of each pass. This can be accomplished by do..while statement. The general form of statement is do Statement; //single statement while (expression); or do { statements to be executed } while (expression);
  • 9. Program 5-8 First 10 Natural Numbers #include <stdio.h> void main() { int i=0; do printf(“%d”, i++); while(i<=9); } #include <stdio.h> void main() { int i=0; do {printf(“%dn”, i); i++; } while(i<=9) ; } #include <stdio.h> void main() { int i=10; do //first execute then check { printf(“%d”, i); i++; } while(i<=9) ; } // prints 10(execute one time) #include <stdio.h> void main() { int i=9; do{ printf(“%d”, i); i--; } while(i>=0) ; }
  • 10. for Statement The for statement is the third and perhaps the most commonly used looping statement in C. This statement includes an expression that specifies an initial value for an index, another expression that determines whether or not the loop is continued, and a third expression that allows the index to be modified at the end of each pass. The general form of statement is for (expr1; expr2; expr3) statement; or { statements }
  • 11. Print 1 to 10 Program 9-12 #include<stdio.h> #include<stdio.h> void main() void main() {int i; { for(i=1;i<=10;i++) for(int i=1;i<=10;++i) printf(“%d”,i); } printf(“%d”,i); } #include<stdio.h> void main() { for(int i=1;i<=10;i=i+1) printf(“%d”,i); } #include<stdio.h> void main() {int i=1; for( ; i<=10; ) { printf(“%d”,i); i++ ; } }//you can also use ++i
  • 12. Program 13-16 #include<stdio.h> void main() {for(int i=0;i<5;i++) { } } // i goes to 5 only #include<stdio.h> void main() { for(i=0;i<5;i++); } // i goes to 5 only Comma Operator #include<stdio.h> void main() { for(i=0,j=0;i<5;i++, j++) { statement1; statement2; statement3; } #include<stdio.h> void main() {int i = 0; for( ; ;) { statements; if(breaking condition) break; i++; }
  • 13. Program 17-18 Reverse Number #include<stdio.h> void main() {n, r, rn=0; scanf(“%d”, &n); while (n!=0) { r=n%10; n=n/10 ; rn=rn*10+r; } printf(“Reverese Number =%d”, rn); } Palindrome Number #include<stdio.h> void main() {n, r, T, rn=0; scanf(“%d”, &n); T=n; while (T!=0) { r=T%10; T=T/10 ; rn=rn*10+r; } if (rn==n) printf(“Palindrome Number); else printf(“Non-Palindrome Number); }
  • 14. Program 19 Prime Number #include<stdio.h> void main() {n, i; scanf(“%d”, &n); while (n % i != 0) i++; if (n==i) printf(“%d is prime”, n); }
  • 15. If you have any queries you can contact me at : rozygag@yahoo.com