SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Loops in C
Definition
 In any programming language, loops
are used to execute a set of
statements repeatedly until a
particular condition is satisfied.
 The loops in C language are used to
execute a block of code or a part of
the program several times.
 In other words, it iterates a code or
group of code many times.
Why use loops in C language?
 Suppose that you have to print table of
2, then you need to write 10 lines of
code.
 By using the loop statement, you can
do it by 2 or 3 lines of code only.
Advantage of loops in C
 1) It saves code.
 2) It helps to traverse the elements of
array.
How it works?
A sequence of statements are executed until a specified
condition is true. This sequence of statements to be executed is
kept inside the curly braces { } known as the Loop body. After
every execution of loop body, condition is verified, and if it is
found to be true the loop body is executed again. When the
condition check returns false, the loop body is not executed.
Types of Loops in C
There are 3 types of loops in C
language:
 while loop
 for loop
 do-while loop
While Loop
while loop can be addressed as
an entry control loop. It is completed
in 3 steps:
 Variable initialization.( e.g int x=0; )
 condition( e.g while( x<=10) )
 Variable increment or decrement ( x++
or x-- or x=x+2 )
 Syntax:
Flowchart:
Example
 Program to print first 10 natural
numbers
Program to print table for the
given number using while loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10)
{
printf("%d n",(number*i));
i++;
}
getch();
}
Infinitive while loop in C
 If you pass 1 as a expression in while
loop, it will run infinite number of
times.
For Loop
 for loop is used to execute a set of
statements repeatedly until a
particular condition is satisfied. we can
say it an open ended loop.
 Syntax:
 In for loop we have exactly two semicolons,
one after initialization and second after
condition. In this loop we can have more than
one initialization or increment/decrement,
separated using comma operator. for loop
can have only one condition.
 The for loop is executed as follows:
◦ It first evaluates the initialization code.
◦ Then it checks the condition expression.
◦ If it is true, it executes the for-loop body.
◦ Then it evaluate the increment/decrement
condition and again follows from step 2.
◦ When the condition expression becomes false, it
exits the loop.
Flowchart:
Example:
 Program to print first n natural
numbers
Print table for the given number
using for loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d n",(number*i));
}
getch();
}
Nested For loop
 We can also have nested for loops, i.e
one for loop inside another for loop.
 Syntax:
Example:
 Program to print half pyramid of
numbers
Infinitive for loop in C
 If you don't initialize any variable, check
condition and increment or decrement
variable in for loop, it is known as infinitive
for loop.
 In other words, if you place 2 semicolons in
for loop, it is known as infinitive for loop.
 If you run this program, you will see above
statement infinite times.
Do While Loop
 In some situations it is necessary to execute
body of the loop before testing the condition.
Such situations can be handled with the help
of do-while loop. do statement evaluates the
body of the loop first and at the end, the
condition is checked using while statement. It
means that for at least one time ,the body of
the loop will be executed, even though the
starting condition inside while is initialized to
false.
 Syntax:
Flowchart:
Example:
 Program to print first ten multiples of
5.
Program to print table for the
given number using do while
loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d n",(number*i));
i++;
}while(i<=10);
getch();
}
Infinitive do while loop
If you pass 1 as a expression in do
while loop, it will run infinite number of
times.

Weitere ähnliche Inhalte

Was ist angesagt?

Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
Jeya Lakshmi
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
ErumShammim
 

Was ist angesagt? (20)

Loops in C
Loops in CLoops in C
Loops in C
 
While loop
While loopWhile loop
While loop
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
The Loops
The LoopsThe Loops
The Loops
 
C tokens
C tokensC tokens
C tokens
 
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
 
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Forloop
ForloopForloop
Forloop
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 

Ähnlich wie Loops in c language

C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 

Ähnlich wie Loops in c language (20)

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
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
C operators
C operatorsC operators
C operators
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
while loop in C.pptx
while loop in C.pptxwhile loop in C.pptx
while loop in C.pptx
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Ch3 repetition
Ch3 repetitionCh3 repetition
Ch3 repetition
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Loops in c
Loops in cLoops in c
Loops in c
 

Mehr von tanmaymodi4

Mehr von tanmaymodi4 (9)

Cryptocurrency
CryptocurrencyCryptocurrency
Cryptocurrency
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Kürzlich hochgeladen (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

Loops in c language

  • 2. Definition  In any programming language, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.  The loops in C language are used to execute a block of code or a part of the program several times.  In other words, it iterates a code or group of code many times.
  • 3. Why use loops in C language?  Suppose that you have to print table of 2, then you need to write 10 lines of code.  By using the loop statement, you can do it by 2 or 3 lines of code only. Advantage of loops in C  1) It saves code.  2) It helps to traverse the elements of array.
  • 4. How it works? A sequence of statements are executed until a specified condition is true. This sequence of statements to be executed is kept inside the curly braces { } known as the Loop body. After every execution of loop body, condition is verified, and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is not executed.
  • 5. Types of Loops in C There are 3 types of loops in C language:  while loop  for loop  do-while loop
  • 6. While Loop while loop can be addressed as an entry control loop. It is completed in 3 steps:  Variable initialization.( e.g int x=0; )  condition( e.g while( x<=10) )  Variable increment or decrement ( x++ or x-- or x=x+2 )  Syntax:
  • 8. Example  Program to print first 10 natural numbers
  • 9. Program to print table for the given number using while loop #include <stdio.h> #include <conio.h> void main(){ int i=1,number=0; clrscr(); printf("Enter a number: "); scanf("%d",&number); while(i<=10) { printf("%d n",(number*i)); i++; } getch(); }
  • 10. Infinitive while loop in C  If you pass 1 as a expression in while loop, it will run infinite number of times.
  • 11. For Loop  for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. we can say it an open ended loop.  Syntax:
  • 12.  In for loop we have exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop can have only one condition.  The for loop is executed as follows: ◦ It first evaluates the initialization code. ◦ Then it checks the condition expression. ◦ If it is true, it executes the for-loop body. ◦ Then it evaluate the increment/decrement condition and again follows from step 2. ◦ When the condition expression becomes false, it exits the loop.
  • 14. Example:  Program to print first n natural numbers
  • 15. Print table for the given number using for loop #include <stdio.h> #include <conio.h> void main(){ int i=1,number=0; clrscr(); printf("Enter a number: "); scanf("%d",&number); for(i=1;i<=10;i++) { printf("%d n",(number*i)); } getch(); }
  • 16. Nested For loop  We can also have nested for loops, i.e one for loop inside another for loop.  Syntax:
  • 17. Example:  Program to print half pyramid of numbers
  • 18. Infinitive for loop in C  If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop.  In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.  If you run this program, you will see above statement infinite times.
  • 19. Do While Loop  In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. It means that for at least one time ,the body of the loop will be executed, even though the starting condition inside while is initialized to false.  Syntax:
  • 21. Example:  Program to print first ten multiples of 5.
  • 22. Program to print table for the given number using do while loop #include <stdio.h> #include <conio.h> void main(){ int i=1,number=0; clrscr(); printf("Enter a number: "); scanf("%d",&number); do{ printf("%d n",(number*i)); i++; }while(i<=10); getch(); }
  • 23. Infinitive do while loop If you pass 1 as a expression in do while loop, it will run infinite number of times.