SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
C++ Loops
What are Loops in C++?
There may arise some situations during programming where programmers
need to execute a block of code several times (with slight variations
sometimes). In general, statements get executed sequentially with a C++
program, one statement followed by another. C++ provides statements for
several control structures along with iteration/repetition capability that allows
programmers to execute a statement or group of statements multiple times.
C++ supports following types of loops:
 while loops
 do while loops
 for loops
All are slightly different and provides loops for different situations.
Figure - Flowchart of Looping:
FOR - for loops are the most useful type. The syntax for a for loop is
for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}
The variable initialization allows you to either declare a variable and give it a
value or give a value to an already existing variable. Second, the condition
tells the program that while the conditional expression is true the loop should
continue to repeat itself. The variable update section is the easiest way for a
for loop to handle changing of the variable. It is possible to do things like x++,
x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could
call other functions that do nothing to the variable but still have a useful effect
on the code. Notice that a semicolon separates each of these sections, that
is important. Also note that every single one of the sections may be empty,
though the semicolons still have to be there. If the condition is empty, it is
evaluated as true and the loop will repeat until something else stops it.
void main()
{
for (i = 1; i <= 10; i++)
{
Cout<< "Hello Worldn";
}
}
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;
}
void main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6)
{
printf( "Hello Worldn");
// update expression
i++;
}
}
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);
void main()
{
int i = 2; // Initialization expression
do
{
// loop body
cout << "Hello Worldn";
// update expression
i++;
} while (i < 1); // test expression
}

Mais conteúdo relacionado

Mais procurados

Looping and Switchcase BDCR
Looping and Switchcase BDCRLooping and Switchcase BDCR
Looping and Switchcase BDCR
beriver
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
Jd Mercado
 
PRESENTATION ON FOR LOOP
PRESENTATION  ON FOR LOOPPRESENTATION  ON FOR LOOP
PRESENTATION ON FOR LOOP
Farooq Joyia
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

Mais procurados (20)

Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Looping and Switchcase BDCR
Looping and Switchcase BDCRLooping and Switchcase BDCR
Looping and Switchcase BDCR
 
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
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
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
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
PRESENTATION ON FOR LOOP
PRESENTATION  ON FOR LOOPPRESENTATION  ON FOR LOOP
PRESENTATION ON FOR LOOP
 
While loop
While loopWhile loop
While loop
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
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 CLooping in C
Looping in C
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Basic for Loop in C
Basic for Loop in CBasic for Loop in C
Basic for Loop in C
 
Iteration
IterationIteration
Iteration
 
Loops in C
Loops in CLoops in C
Loops in C
 
Java loops
Java loopsJava loops
Java loops
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Loops
LoopsLoops
Loops
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 

Semelhante a Loops in c++

3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
Hardik gupta
 

Semelhante a Loops in c++ (20)

Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
itretion.docx
itretion.docxitretion.docx
itretion.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
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Loops c++
Loops c++Loops c++
Loops c++
 
C language 2
C language 2C language 2
C language 2
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptx
 
Loops
LoopsLoops
Loops
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
M C6java6
M C6java6M C6java6
M C6java6
 
Loop structures
Loop structuresLoop structures
Loop structures
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Último (20)

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Loops in c++

  • 1. C++ Loops What are Loops in C++? There may arise some situations during programming where programmers need to execute a block of code several times (with slight variations sometimes). In general, statements get executed sequentially with a C++ program, one statement followed by another. C++ provides statements for several control structures along with iteration/repetition capability that allows programmers to execute a statement or group of statements multiple times. C++ supports following types of loops:  while loops  do while loops  for loops All are slightly different and provides loops for different situations. Figure - Flowchart of Looping:
  • 2. FOR - for loops are the most useful type. The syntax for a for loop is for ( variable initialization; condition; variable update ) { Code to execute while the condition is true } The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code. Notice that a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. If the condition is empty, it is evaluated as true and the loop will repeat until something else stops it.
  • 3. void main() { for (i = 1; i <= 10; i++) { Cout<< "Hello Worldn"; } } 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; } void main() { // initialization expression int i = 1;
  • 4. // test expression while (i < 6) { printf( "Hello Worldn"); // update expression i++; } } 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); void main() { int i = 2; // Initialization expression do {
  • 5. // loop body cout << "Hello Worldn"; // update expression i++; } while (i < 1); // test expression }