SlideShare a Scribd company logo
1 of 13
Loops
Loops in programming come into use when we need to repeatedly execute a
block of statements. For example: Suppose we want to print “Hello World” 10
times. This can be done in two ways as shown below:
Iterative Method
An iterative method to do this is to write the printf() statement 10 times.
// C program to illustrate need of loops
#include <stdio.h>
int main()
{
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
printf( "Hello Worldn");
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Using Loops
In Loop, the statement needs to be written only once and the loop will be
executed 10 times as shown below.
In computer programming, a loop is a sequence of instructions that is repeated
until a certain condition is reached.
 An operation is done, such as getting an item of data and changing it, and
then some condition is checked such as whether a counter has reached a
prescribed number.
 Counter not Reached: If the counter has not reached the desired number,
the next instruction in the sequence returns to the first instruction in the
sequence and repeat it.
 Counter reached: If the condition has been reached, the next instruction
“falls through” to the next sequential instruction or branches outside the loop.
There are mainly two types of loops:
1. Entry Controlled loops: In this type of loops the test condition is tested
before entering the loop body. For Loop and While Loop are entry
controlled loops.
2. Exit Controlled Loops: In this type of loops the test condition is tested or
evaluated at the end of loop body. Therefore, the loop body will execute
atleast once, irrespective of whether the test condition is true or false. do –
while loop is exit controlled loop.
for Loop
A for loop is a repetition control structure which allows us to write a loop that is
executed a specific number of times. The loop enables us to perform n number
of steps together in one line.
Syntax:
for (initialization expr; test expr; update expr)
for(i;i<n;i++)
{
// body of the loop
// statements we want to execute
}
 In for loop, a loop variable is used to control the loop. First initialize this
loop variable to some value, then check whether this variable is less than
or greater than counter value.
 If statement is true, then loop body is executed and loop variable gets
updated . Steps are repeated till exit condition comes.
 Initialization Expression: In this expression we have to initialize the loop
counter to some value. for example: int i=1;
 Test Expression: In this expression we have to test the condition. If the
condition evaluates to true then we will execute the body of loop and go to
update expression otherwise we will exit from the for loop. For example: i <=
10;
 Update Expression: After executing loop body this expression
increments/decrements the loop variable by some value. for example: i++;
Equivalent flow diagram for loop
:
Example:
 C
// C program to illustrate for loop
#include <stdio.h>
int main()
{
int i=0;
for (i = 1; i <= 10; i++)
{
printf( "Hello Worldn");
}
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
While Loop
 While studying for loop we have seen that the number of iterations
is known beforehand, i.e. the number of times the loop body is
needed to be executed is known to us.
 while loops are used in situations where we do not know the exact
number of iterations of loop beforehand. The loop execution is
terminated on the basis of test condition.
Syntax:
We have already stated that a loop is mainly consisted of three statements –
initialization expression, test expression, update expression. The syntax of the
three loops – For, while and do while mainly differs on the placement of these
three statements.
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
Flow Diagram:
Example:
C // C program to illustrate while loop
#include <stdio.h>
int main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6)
{
printf( "Hello Worldn");
// update expression
i++;
}
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
do while loop
In do while loops also the loop execution is terminated on the basis of test
condition.
The main difference between do while loop and while loop is in do while
loop the condition is tested at the end of loop body, i.e do while loop is exit
controlled whereas the other two loops are entry controlled loops.
Note: In do while loop the loop body will execute at least once irrespective of
test condition.
Syntax:
initialization expression;
do
{
// statements
update_expression;
}
while (test_expression);
Note: Notice the semi – colon(“;”) in the end of loop.
Flow Diagram:
Example:
 C
// C program to illustrate do-while loop
#include <stdio.h>
int main()
{
int i = 2; // Initialization expression
do
{
// loop body
printf( "Hello Worldn");
// update expression
i++;
} while (i < 1); // test expression
return 0;
}
Output: Hello World
In the above program the test condition (i<1) evaluates to false. But still as the
loop is exit – controlled the loop body will execute once.
What about an Infinite Loop?
A loop becomes an infinite loop if a condition never becomes false. The for loop is
traditionally used for this purpose. Since none of the three expressions that form the
'for' loop are required, you can make an endless loop by leaving the conditional
expression empty.
#include <stdio.h>
int main () {
for( ; ; ) {
printf("This loop will run forever.n");
}
return 0;
}
When the conditional expression is absent, it is assumed to be true.
You may have an initialization and increment expression, but C
programmers more commonly use the for(;;) construct to signify an
infinite loop.
NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.
An infinite loop (sometimes called an endless loop ) is a piece of coding that
lacks a functional exit so that it repeats indefinitely. An infinite loop occurs when
a condition always evaluates to true. Usually, this is an error.
 C
// C program to demonstrate infinite loops
// using for and while
// Uncomment the sections to see the output
#include <stdio.h>
int main ()
{
int i;
// This is an infinite for loop as the condition
// expression is blank
for ( ; ; )
{
printf("This loop will run forever.n");
}
// This is an infinite for loop as the condition
// given in while loop will keep repeating infinitely
/*
while (i != 0)
{
i-- ;
printf( "This loop will run forever.n");
}
*/
// This is an infinite for loop as the condition
// given in while loop is "true"
/*
while (true)
{
printf( "This loop will run forever.n");
}
*/
}
Output:
This loop will run forever.
This loop will run forever.
...................
SR.NO while loop do-while loop
1. While the loop is an entry control loop
because firstly, the condition is
The do-while loop is an exit control loop
because in this, first of all, the body of
checked, then the loop's body is
executed.
the loop is executed then the condition
is checked true or false.
2. The statement of while loop may not
be executed at all.
The statement of the do-while loop
must be executed at least once.
3. The while loop terminates when the
condition becomes false.
As long as the condition is true, the
compiler keeps executing the loop in
the do-while loop.
4. In a while loop, the test condition
variable must be initialized first to
check the test condition in the loop.
In a do-while loop, the variable of test
condition Initialized in the loop also.
5. In a while loop, at the end of the
condition, there is no semicolon.
Syntax:
while (condition)
In this, at the end of the condition,
there is a semicolon.
Syntax:
while (condition);
6. While loop is not used for creating
menu-driven programs.
It is mostly used for creating menu-
driven programs because at least one
time; the loop is executed whether the
condition is true or false.
7. In a while loop, the number of
executions depends on the condition
defined in the while block.
In a do-while loop, irrespective of the
condition mentioned, a minimum of 1
execution occurs.
8. Syntax of while loop:
while (condition)
{
Block of statements;
}
Statement-x;
Syntax of do-while loop:
do
{
statements;
}
while (condition);
Statement-x;
9. Program of while loop:
Program of while loop:
#include
#include
Void main()
{
int i;
clrscr();
i = 1;
while(i<=10)
{
printf("hello");
i = i + 1;
}
getch();
}
Program of do-while loop:
#include
#include
Void main()
{
int i;
clrscr();
i = 1;
do
{
printf("hello");
i = i + 1;
}
while(i<=10);
getch();
}
10. Flowchart of while loop: Flowchart of do-while loop:
loops and iteration.docx

More Related Content

What's hot (20)

Looping statements
Looping statementsLooping statements
Looping statements
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loops in c
Loops in cLoops in c
Loops in c
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
The Loops
The LoopsThe Loops
The Loops
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
Looping statement
Looping statementLooping statement
Looping statement
 
Forloop
ForloopForloop
Forloop
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
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
 
Looping in c++
Looping in c++Looping in c++
Looping in 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
 
While loop
While loopWhile loop
While loop
 
Iteration Statement in C++
Iteration Statement in C++Iteration Statement in C++
Iteration Statement in C++
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements
 

Similar to loops and iteration.docx

Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion StatementsHuda Alameen
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxVijayKumarLokanadam
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Abou Bakr Ashraf
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptxsujatha629799
 
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 LoopPriyom Majumder
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdfKirubelWondwoson1
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 

Similar to loops and iteration.docx (20)

Loops In C++
Loops In C++Loops In C++
Loops In C++
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
C language 2
C language 2C language 2
C language 2
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
Programming loop
Programming loopProgramming loop
Programming loop
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptx
 
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
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
M C6java6
M C6java6M C6java6
M C6java6
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 

Recently uploaded

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 

Recently uploaded (20)

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

loops and iteration.docx

  • 1. Loops Loops in programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times. This can be done in two ways as shown below: Iterative Method An iterative method to do this is to write the printf() statement 10 times. // C program to illustrate need of loops #include <stdio.h> int main() { printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); printf( "Hello Worldn"); return 0; } Output: Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Using Loops In Loop, the statement needs to be written only once and the loop will be executed 10 times as shown below.
  • 2. In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is reached.  An operation is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.  Counter not Reached: If the counter has not reached the desired number, the next instruction in the sequence returns to the first instruction in the sequence and repeat it.  Counter reached: If the condition has been reached, the next instruction “falls through” to the next sequential instruction or branches outside the loop. There are mainly two types of loops: 1. Entry Controlled loops: In this type of loops the test condition is tested before entering the loop body. For Loop and While Loop are entry controlled loops. 2. Exit Controlled Loops: In this type of loops the test condition is tested or evaluated at the end of loop body. Therefore, the loop body will execute atleast once, irrespective of whether the test condition is true or false. do – while loop is exit controlled loop.
  • 3. for Loop A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. Syntax: for (initialization expr; test expr; update expr) for(i;i<n;i++) { // body of the loop // statements we want to execute }  In for loop, a loop variable is used to control the loop. First initialize this loop variable to some value, then check whether this variable is less than or greater than counter value.  If statement is true, then loop body is executed and loop variable gets updated . Steps are repeated till exit condition comes.  Initialization Expression: In this expression we have to initialize the loop counter to some value. for example: int i=1;  Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of loop and go to update expression otherwise we will exit from the for loop. For example: i <= 10;  Update Expression: After executing loop body this expression increments/decrements the loop variable by some value. for example: i++; Equivalent flow diagram for loop
  • 4. : Example:  C // C program to illustrate for loop #include <stdio.h> int main() { int i=0; for (i = 1; i <= 10; i++) { printf( "Hello Worldn"); } return 0; } Output: Hello World
  • 5. Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World While Loop  While studying for loop we have seen that the number of iterations is known beforehand, i.e. the number of times the loop body is needed to be executed is known to us.  while loops are used in situations where we do not know the exact number of iterations of loop beforehand. The loop execution is terminated on the basis of test condition. Syntax: We have already stated that a loop is mainly consisted of three statements – initialization expression, test expression, update expression. The syntax of the three loops – For, while and do while mainly differs on the placement of these three statements. initialization expression; while (test_expression) { // statements update_expression; }
  • 6. Flow Diagram: Example: C // C program to illustrate while loop #include <stdio.h> int main() { // initialization expression int i = 1; // test expression while (i < 6) { printf( "Hello Worldn"); // update expression i++; } return 0; } Output: Hello World Hello World Hello World
  • 7. Hello World Hello World do while loop In do while loops also the loop execution is terminated on the basis of test condition. The main difference between do while loop and while loop is in do while loop the condition is tested at the end of loop body, i.e do while loop is exit controlled whereas the other two loops are entry controlled loops. Note: In do while loop the loop body will execute at least once irrespective of test condition. Syntax: initialization expression; do { // statements update_expression; } while (test_expression); Note: Notice the semi – colon(“;”) in the end of loop.
  • 8. Flow Diagram: Example:  C // C program to illustrate do-while loop #include <stdio.h> int main() { int i = 2; // Initialization expression do { // loop body printf( "Hello Worldn"); // update expression i++; } while (i < 1); // test expression return 0; } Output: Hello World
  • 9. In the above program the test condition (i<1) evaluates to false. But still as the loop is exit – controlled the loop body will execute once. What about an Infinite Loop? A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the 'for' loop are required, you can make an endless loop by leaving the conditional expression empty. #include <stdio.h> int main () { for( ; ; ) { printf("This loop will run forever.n"); } return 0; } When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop. NOTE − You can terminate an infinite loop by pressing Ctrl + C keys. An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats indefinitely. An infinite loop occurs when a condition always evaluates to true. Usually, this is an error.  C // C program to demonstrate infinite loops // using for and while // Uncomment the sections to see the output #include <stdio.h>
  • 10. int main () { int i; // This is an infinite for loop as the condition // expression is blank for ( ; ; ) { printf("This loop will run forever.n"); } // This is an infinite for loop as the condition // given in while loop will keep repeating infinitely /* while (i != 0) { i-- ; printf( "This loop will run forever.n"); } */ // This is an infinite for loop as the condition // given in while loop is "true" /* while (true) { printf( "This loop will run forever.n"); } */ } Output: This loop will run forever. This loop will run forever. ................... SR.NO while loop do-while loop 1. While the loop is an entry control loop because firstly, the condition is The do-while loop is an exit control loop because in this, first of all, the body of
  • 11. checked, then the loop's body is executed. the loop is executed then the condition is checked true or false. 2. The statement of while loop may not be executed at all. The statement of the do-while loop must be executed at least once. 3. The while loop terminates when the condition becomes false. As long as the condition is true, the compiler keeps executing the loop in the do-while loop. 4. In a while loop, the test condition variable must be initialized first to check the test condition in the loop. In a do-while loop, the variable of test condition Initialized in the loop also. 5. In a while loop, at the end of the condition, there is no semicolon. Syntax: while (condition) In this, at the end of the condition, there is a semicolon. Syntax: while (condition); 6. While loop is not used for creating menu-driven programs. It is mostly used for creating menu- driven programs because at least one time; the loop is executed whether the condition is true or false. 7. In a while loop, the number of executions depends on the condition defined in the while block. In a do-while loop, irrespective of the condition mentioned, a minimum of 1 execution occurs. 8. Syntax of while loop: while (condition) { Block of statements; } Statement-x; Syntax of do-while loop: do { statements; } while (condition); Statement-x;
  • 12. 9. Program of while loop: Program of while loop: #include #include Void main() { int i; clrscr(); i = 1; while(i<=10) { printf("hello"); i = i + 1; } getch(); } Program of do-while loop: #include #include Void main() { int i; clrscr(); i = 1; do { printf("hello"); i = i + 1; } while(i<=10); getch(); } 10. Flowchart of while loop: Flowchart of do-while loop: