際際滷

際際滷Share a Scribd company logo
LOOP CONSTRUCTS
IN C
WHAT IS LOOP CONSTRUCT IN C ?
 A LOOP IS A REPETITION CONTROL
STRUCTURE THAT ALLOWS YOU TO EFFICIENTLY
WRITE A LOOP THAT NEEDS TO EXECUTE A SPECIFIC
NUMBER OF TIMES.
C programming has three types of
loops:
1.for loop
2.while loop
3.do...while loop
1.FOR LOOP
 BASIC SYNTAX OF FOR LOOP
 FLOW DIAGRAM
2. WHILE LOOP
 BASIC SYNTAX OF WHILE LOOP
 FLOW DIAGRAM
3. DO WHILE LOOP
BASIC SYNTAX OF DO WHILE LOOP
 FLOW DIAGRAM
WHILE LOOP CONSTRUCT
IN C
A WHILE LOOP IN C PROGRAMMING REPEATEDLY EXECUTES A
TARGET STATEMENT AS LONG AS A GIVEN CONDITION IS TRUE
 THE SYNTAX OF THE WHILE LOOP IS:
WHILE (TESTEXPRESSION)
{
// THE BODY OF THE LOOP
}
SYNTAX OF THE WHILE LOOP
HOW WHILE LOOP WORKS
 THE WHILE LOOP EVALUATES THE TEST EXPRESSION INSIDE THE
PARENTHESES ().
 IF TEST EXPRESSION IS TRUE, STATEMENTS INSIDE THE BODY OF WHILE LOOP
ARE EXECUTED. THEN, TEST EXPRESSION IS EVALUATED AGAIN.
 THE PROCESS GOES ON UNTIL TEST EXPRESSION IS EVALUATED TO FALSE.
 IF TEST EXPRESSION IS FALSE, THE LOOP TERMINATES (ENDS).
 TO LEARN MORE ABOUT TEST EXPRESSIONS (WHEN TEST EXPRESSION IS
EVALUATED TO TRUE AND FALSE), CHECK OUT RELATIONAL AND LOGICAL
OPERATORS.
 FLOWCHART OF WHILE LOOP
EXAMPLE 1- WHILE LOOP
 // PRINT NUMBERS FROM 1 TO 5
#include<stdio.h>
int main()
{
int i = 1;
while (i <= 5) {
printf("%dn", i);
++i; }
return 0;
}
OUTPUT
1 2 3 4 5
 HERE, WE HAVE INITIALIZED I TO 1.
 WHEN I = 1, THE TEST EXPRESSION I <= 5 IS TRUE. HENCE, THE BODY OF
THE WHILE LOOP IS EXECUTED. THIS PRINTS 1 ON THE SCREEN AND THE
VALUE OF I IS INCREASED TO 2.
 NOW, I = 2, THE TEST EXPRESSION I <= 5 IS AGAIN TRUE. THE BODY OF
THE WHILE LOOP IS EXECUTED AGAIN. THIS PRINTS 2 ON THE SCREEN AND THE
VALUE OF I IS INCREASED TO 3.
 THIS PROCESS GOES ON UNTIL I BECOMES 6. THEN, THE TEST EXPRESSION I
<= 5 WILL BE FALSE AND THE LOOP TERMINATES.
Loops
Imagine teacher asked you to print numbers from 1
to 100, it might be time consuming but you may do
it. But what if she asked you to print 1 lakh or more
numbers?
Will you use the printf statement 1 lakh times to print
it?
OF COURSE NOT !
We humans always try to find a way to make our task
simple and quickly.
And to solve this problem we have created the for
loop.
So what actually is a for loop?
A for loop is just an advanced form of while
loop where we have to initialize, check if the
condition is true or false, execute the code
and update counter in the beginning itself.
Lets understanding
FOR LOOP using an
Analogy.
We All have played
this game but did we
realise that the birds
here try to follow a
loop?
Every bird here waits for
its turn and when called it
strikes and the same
process is repeated for
other birds
But the number of birds are
limited and hence we need to
know this since the beginning
other wise we will run out of
birds and pigs remains
undestroyed.
Similarly
Similarly in FOR LOOP we
have to write when do we
need to stop at the very
beginning to prevent an
infinite loop formation.
for (birds =1; birds <= 3;
birds++)
{
launch the bird;
}
A for loop consist of three elementary steps :
 The initialization (start)
 The Test Expression(tells when to continue or
stop the loop)
 The Update counter. (updates after every
iteration)
for (int i = 0 ; i <= limit ; i++/i--
etc.)
{
execution of code ;
}
data type
variabl
e conditional
operator
updating data
using
increment or
decrement
operator
curly
braces
1) Start
2) write the starting digit and the
condition along with how you
want to update the counter.
3) Use curly braces to enclose the
code of for loop.
4) Inside the curly braces type the
code you want to execute.
5) On running the program ,
compiler will check the condition
and if its true, it will execute the
code and update the counter.
6) The step 5 is repeated until the
condition remains true.
7) If the condition becomes false,
the compiler stops printing the
code and comes out of the loop.
Understanding the working.
DO-WHILE LOOP CONSTRUCT IN C
LANGUAGE
 THE C DO-WHILE STATEMENT CREATES A
STRUCTURED LOOP THAT EXECUTES AS LONG AS A
SPECIFIED CONDITION IS TRUE AT THE END OF EACH
PASS THROUGH THE LOOP, AND IF THE VALUE OF
EXPRESSION IS FALSE THEN THE LOOP IS EXITED.
SYNTAX OF A DO-WHILE LOOP
 THE SYNTAX OF A D0-WHILE LOOP IN THE C
PROGRAMMING LANGUAGE IS:
Initialize;
do
{
statement(s);
increment/decrement;
}
while (condition);
FLOWCHART OF DO WHILE LOOP IN C.
WORKING OF A DO-WHILE LOOP:
 The do-while loop is similar to the while loop with one
important difference. The body of do-while loop is executed
at least once. Only then, the test expression is evaluated.
 If test Expression is true, the body of the loop is executed
again and test Expression is evaluated once more.
 This process goes on until test Expression becomes false.
 If test Expression is false, the loop ends.
AN EXAMPLE OF DO-WHILE
LOOP WITH A SIMPLE CODE
The following programme prints the numbers
between 1 to 100 which are the multiples of n
(n is the number given by user) using do while
loop:
#include<stdio.h>
int main()
{
int i = 1,n; // declare and initialize i to 1
printf("Enter the number of which you want
the multiples.n=");
scanf("%d",&n);
do //the following is the body of loop
{
// check whether i is multiple of n or not
if(i % n == 0)
printf("%d ", i);
i++; // increment i by 1
}while(i < 100); // stop the loop when i
becomes greater than 100
return 0;
}
Loops
Loops
Loops
Loops
Loops
Loops
Loops
A "While" Loop is used
to repeat a specific
block of code an
unknown number of
times, until a condition
Advantages
Disadvantages
While loop can cause the problem if
the index length is incorrect.
It is also slow as the compiler adds
the runtime code to perform the
conditional check on every iteration
through this loop.
Loops
A do while loop is a control flow
statement that executes a block of
code at least once, and then
repeatedly executes the block, or
not, depending on a given
Boolean condition at the end of
the block
Advantages
Disdvantage
1.Condition must be updated
within body of loop.
2. Caution must be used to
avoid an infinite loop.
Loops
Advantages
How many times the loop will
execute before the loop starts is
for loop.
Dissadvantages
It cannot be used for accessing or
viewing any specific element of an
array or collection like array and string
etc.
Its used when you want to traverse
through whole of the array or
collection framework you are using in
your program
Loops
Loops
Loops
Loops
Loops
Loops
Loops
Loops

More Related Content

What's hot (20)

PPT
Programming loop
University of Potsdam
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
PPT
Looping in c++
deekshagopaliya
PPT
Looping in c++
deekshagopaliya
PPTX
types of loops and what is loop
waheed dogar
PPTX
While , For , Do-While Loop
Abhishek Choksi
PPTX
Looping statements
AbhishekMondal42
PPTX
Loops in c programming
CHANDAN KUMAR
PPTX
TDD in Powershell
Michael Willis
PPTX
Loops in C Programming Language
Mahantesh Devoor
PPTX
C++ loop
Khelan Ameen
PPTX
Loops in c language
tanmaymodi4
PDF
Function procedure c6 c7
Omar Al-Sabek
PPT
170120107074 looping statements and nesting of loop statements
harsh kothari
PDF
Control structures c2 c3
Omar Al-Sabek
PPTX
Types of loops in c language
sneha2494
PPT
While loop
Feras_83
PPTX
Chapter 5.1
sotlsoc
Programming loop
University of Potsdam
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
Looping in c++
deekshagopaliya
Looping in c++
deekshagopaliya
types of loops and what is loop
waheed dogar
While , For , Do-While Loop
Abhishek Choksi
Looping statements
AbhishekMondal42
Loops in c programming
CHANDAN KUMAR
TDD in Powershell
Michael Willis
Loops in C Programming Language
Mahantesh Devoor
C++ loop
Khelan Ameen
Loops in c language
tanmaymodi4
Function procedure c6 c7
Omar Al-Sabek
170120107074 looping statements and nesting of loop statements
harsh kothari
Control structures c2 c3
Omar Al-Sabek
Types of loops in c language
sneha2494
While loop
Feras_83
Chapter 5.1
sotlsoc

Similar to Loops (20)

DOCX
loops and iteration.docx
JavvajiVenkat
PDF
Loop and while Loop
JayBhavsar68
PPTX
Loops in c
RekhaBudhwar
DOCX
itretion.docx
JavvajiVenkat
PPT
Chapter06.PPT
vamsiKrishnasai3
PPTX
Decision Making and Looping
Munazza-Mah-Jabeen
PPTX
Loops Basics
Mushiii
PPTX
C Programming: Looping Statements in C Pgm
Navya Francis
PPTX
Loop structures
tazeem sana
PPTX
Loops In C++
Banasthali Vidyapith
PDF
3. Flow Controls in C (Part II).pdf
santosh147365
PPTX
C loops
sunilchute1
PPT
12 lec 12 loop
kapil078
PDF
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
PPT
Lecture 13 Loops1 with C++ programming.PPT
SamahAdel16
PPTX
Loops in C.pptx
nagalakshmig4
PPTX
Loops in c language
Tanmay Modi
PPTX
Loop in C Properties & Applications
Emroz Sardar
PPTX
COM1407: Program Control Structures Repetition and Loops
Hemantha Kulathilake
loops and iteration.docx
JavvajiVenkat
Loop and while Loop
JayBhavsar68
Loops in c
RekhaBudhwar
itretion.docx
JavvajiVenkat
Chapter06.PPT
vamsiKrishnasai3
Decision Making and Looping
Munazza-Mah-Jabeen
Loops Basics
Mushiii
C Programming: Looping Statements in C Pgm
Navya Francis
Loop structures
tazeem sana
Loops In C++
Banasthali Vidyapith
3. Flow Controls in C (Part II).pdf
santosh147365
C loops
sunilchute1
12 lec 12 loop
kapil078
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
Lecture 13 Loops1 with C++ programming.PPT
SamahAdel16
Loops in C.pptx
nagalakshmig4
Loops in c language
Tanmay Modi
Loop in C Properties & Applications
Emroz Sardar
COM1407: Program Control Structures Repetition and Loops
Hemantha Kulathilake
Ad

Recently uploaded (20)

PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
PPTX
ManageIQ - Sprint 264 Review - 際際滷 Deck
ManageIQ
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
PPTX
For my supp to finally picking supp that work
necas19388
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
PDF
Dealing with JSON in the relational world
Andres Almiray
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
PDF
Rewards and Recognition (2).pdf
ethan Talor
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ManageIQ - Sprint 264 Review - 際際滷 Deck
ManageIQ
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
For my supp to finally picking supp that work
necas19388
Automated Test Case Repair Using Language Models
Lionel Briand
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
>Wondershare Filmora Crack Free Download 2025
utfefguu
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
Dealing with JSON in the relational world
Andres Almiray
computer forensics encase emager app exp6 1.pptx
ssuser343e92
Building scalbale cloud native apps with .NET 8
GillesMathieu10
Rewards and Recognition (2).pdf
ethan Talor
Ad

Loops

  • 2. WHAT IS LOOP CONSTRUCT IN C ? A LOOP IS A REPETITION CONTROL STRUCTURE THAT ALLOWS YOU TO EFFICIENTLY WRITE A LOOP THAT NEEDS TO EXECUTE A SPECIFIC NUMBER OF TIMES.
  • 3. C programming has three types of loops: 1.for loop 2.while loop 3.do...while loop
  • 4. 1.FOR LOOP BASIC SYNTAX OF FOR LOOP
  • 6. 2. WHILE LOOP BASIC SYNTAX OF WHILE LOOP
  • 8. 3. DO WHILE LOOP BASIC SYNTAX OF DO WHILE LOOP
  • 10. WHILE LOOP CONSTRUCT IN C A WHILE LOOP IN C PROGRAMMING REPEATEDLY EXECUTES A TARGET STATEMENT AS LONG AS A GIVEN CONDITION IS TRUE
  • 11. THE SYNTAX OF THE WHILE LOOP IS: WHILE (TESTEXPRESSION) { // THE BODY OF THE LOOP } SYNTAX OF THE WHILE LOOP
  • 12. HOW WHILE LOOP WORKS THE WHILE LOOP EVALUATES THE TEST EXPRESSION INSIDE THE PARENTHESES (). IF TEST EXPRESSION IS TRUE, STATEMENTS INSIDE THE BODY OF WHILE LOOP ARE EXECUTED. THEN, TEST EXPRESSION IS EVALUATED AGAIN. THE PROCESS GOES ON UNTIL TEST EXPRESSION IS EVALUATED TO FALSE. IF TEST EXPRESSION IS FALSE, THE LOOP TERMINATES (ENDS). TO LEARN MORE ABOUT TEST EXPRESSIONS (WHEN TEST EXPRESSION IS EVALUATED TO TRUE AND FALSE), CHECK OUT RELATIONAL AND LOGICAL OPERATORS.
  • 13. FLOWCHART OF WHILE LOOP
  • 14. EXAMPLE 1- WHILE LOOP // PRINT NUMBERS FROM 1 TO 5 #include<stdio.h> int main() { int i = 1; while (i <= 5) { printf("%dn", i); ++i; } return 0; } OUTPUT 1 2 3 4 5
  • 15. HERE, WE HAVE INITIALIZED I TO 1. WHEN I = 1, THE TEST EXPRESSION I <= 5 IS TRUE. HENCE, THE BODY OF THE WHILE LOOP IS EXECUTED. THIS PRINTS 1 ON THE SCREEN AND THE VALUE OF I IS INCREASED TO 2. NOW, I = 2, THE TEST EXPRESSION I <= 5 IS AGAIN TRUE. THE BODY OF THE WHILE LOOP IS EXECUTED AGAIN. THIS PRINTS 2 ON THE SCREEN AND THE VALUE OF I IS INCREASED TO 3. THIS PROCESS GOES ON UNTIL I BECOMES 6. THEN, THE TEST EXPRESSION I <= 5 WILL BE FALSE AND THE LOOP TERMINATES.
  • 17. Imagine teacher asked you to print numbers from 1 to 100, it might be time consuming but you may do it. But what if she asked you to print 1 lakh or more numbers? Will you use the printf statement 1 lakh times to print it? OF COURSE NOT ! We humans always try to find a way to make our task simple and quickly. And to solve this problem we have created the for loop. So what actually is a for loop? A for loop is just an advanced form of while loop where we have to initialize, check if the condition is true or false, execute the code and update counter in the beginning itself. Lets understanding FOR LOOP using an Analogy.
  • 18. We All have played this game but did we realise that the birds here try to follow a loop? Every bird here waits for its turn and when called it strikes and the same process is repeated for other birds But the number of birds are limited and hence we need to know this since the beginning other wise we will run out of birds and pigs remains undestroyed. Similarly Similarly in FOR LOOP we have to write when do we need to stop at the very beginning to prevent an infinite loop formation.
  • 19. for (birds =1; birds <= 3; birds++) { launch the bird; } A for loop consist of three elementary steps : The initialization (start) The Test Expression(tells when to continue or stop the loop) The Update counter. (updates after every iteration)
  • 20. for (int i = 0 ; i <= limit ; i++/i-- etc.) { execution of code ; } data type variabl e conditional operator updating data using increment or decrement operator curly braces
  • 21. 1) Start 2) write the starting digit and the condition along with how you want to update the counter. 3) Use curly braces to enclose the code of for loop. 4) Inside the curly braces type the code you want to execute. 5) On running the program , compiler will check the condition and if its true, it will execute the code and update the counter. 6) The step 5 is repeated until the condition remains true. 7) If the condition becomes false, the compiler stops printing the code and comes out of the loop. Understanding the working.
  • 22. DO-WHILE LOOP CONSTRUCT IN C LANGUAGE THE C DO-WHILE STATEMENT CREATES A STRUCTURED LOOP THAT EXECUTES AS LONG AS A SPECIFIED CONDITION IS TRUE AT THE END OF EACH PASS THROUGH THE LOOP, AND IF THE VALUE OF EXPRESSION IS FALSE THEN THE LOOP IS EXITED.
  • 23. SYNTAX OF A DO-WHILE LOOP THE SYNTAX OF A D0-WHILE LOOP IN THE C PROGRAMMING LANGUAGE IS: Initialize; do { statement(s); increment/decrement; } while (condition);
  • 24. FLOWCHART OF DO WHILE LOOP IN C.
  • 25. WORKING OF A DO-WHILE LOOP: The do-while loop is similar to the while loop with one important difference. The body of do-while loop is executed at least once. Only then, the test expression is evaluated. If test Expression is true, the body of the loop is executed again and test Expression is evaluated once more. This process goes on until test Expression becomes false. If test Expression is false, the loop ends.
  • 26. AN EXAMPLE OF DO-WHILE LOOP WITH A SIMPLE CODE The following programme prints the numbers between 1 to 100 which are the multiples of n (n is the number given by user) using do while loop:
  • 27. #include<stdio.h> int main() { int i = 1,n; // declare and initialize i to 1 printf("Enter the number of which you want the multiples.n="); scanf("%d",&n); do //the following is the body of loop { // check whether i is multiple of n or not if(i % n == 0) printf("%d ", i); i++; // increment i by 1 }while(i < 100); // stop the loop when i becomes greater than 100 return 0; }
  • 35. A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition Advantages
  • 36. Disadvantages While loop can cause the problem if the index length is incorrect. It is also slow as the compiler adds the runtime code to perform the conditional check on every iteration through this loop.
  • 38. A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condition at the end of the block Advantages
  • 39. Disdvantage 1.Condition must be updated within body of loop. 2. Caution must be used to avoid an infinite loop.
  • 41. Advantages How many times the loop will execute before the loop starts is for loop.
  • 42. Dissadvantages It cannot be used for accessing or viewing any specific element of an array or collection like array and string etc. Its used when you want to traverse through whole of the array or collection framework you are using in your program