SlideShare a Scribd company logo
1 of 20
Object Oriented Programming: 18
Control Structures Looping
Prof Neeraj Bhargava
Vaibhav Khanna
Department of Computer Science
School of Engineering and Systems Sciences
Maharshi Dayanand Saraswati University Ajmer
2
for Looping (Repetition) Structure
• The general form of the for statement is:
• The initial statement, loop
condition, and update statement are
called for loop control statements
– initial statement usually initializes a
variable (called the for loop control, or for
indexed, variable)
• In C++, for is a reserved word
3
for Looping (Repetition) Structure
(continued)
4
for Looping (Repetition) Structure
(continued)
5
for Looping (Repetition) Structure
(continued)
• C++ allows you to use fractional values for
loop control variables of the double type
– Results may differ
• The following is a semantic error:
• The following is a legal for loop:
for (;;)
cout << "Hello" << endl;
6
for Looping (Repetition) Structure
(continued)
7
do…while Looping (Repetition)
Structure
• General form of a do...while:
• The statement executes first, and then the
expression is evaluated
• To avoid an infinite loop, body must contain a
statement that makes the expression false
• The statement can be simple or compound
• Loop always iterates at least once
8
do…while Looping (Repetition)
Structure (continued)
9
do…while Looping (Repetition)
Structure (continued)
11
Divisibility Test by 3 and 9
12
Choosing the Right Looping Structure
• All three loops have their place in C++
– If you know or can determine in advance the
number of repetitions needed, the for loop is
the correct choice
– If you do not know and cannot determine in
advance the number of repetitions needed, and it
could be zero, use a while loop
– If you do not know and cannot determine in
advance the number of repetitions needed, and it
is at least one, use a do...while loop
13
break and continue Statements
• break and continue alter the flow of
control
• break statement is used for two purposes:
– To exit early from a loop
• Can eliminate the use of certain (flag) variables
– To skip the remainder of the switch structure
• After the break statement executes, the
program continues with the first statement
after the structure
14
break & continue Statements
(continued)
• continue is used in while, for, and
do…while structures
• When executed in a loop
– It skips remaining statements and proceeds
with the next iteration of the loop
15
Nested Control Structures
• To create the following pattern:
*
**
***
****
*****
• We can use the following code:
for (i = 1; i <= 5 ; i++)
{
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
16
Nested Control Structures (continued)
• What is the result if we replace the first for
statement with the following?
for (i = 5; i >= 1; i--)
• Answer:
*****
****
***
**
*
17
Summary
• C++ has three looping (repetition) structures:
– while, for, and do…while
• while, for, and do are reserved words
• while and for loops are called pretest
loops
• do...while loop is called a posttest loop
• while and for may not execute at all, but
do...while always executes at least once
18
Summary (continued)
• while: expression is the decision maker,
and the statement is the body of the loop
• A while loop can be:
– Counter-controlled
– Sentinel-controlled
– EOF-controlled
• In the Windows console environment, the
end-of-file marker is entered using
Ctrl+z
19
Summary (continued)
• for loop: simplifies the writing of a
counter-controlled while loop
– Putting a semicolon at the end of the for loop
is a semantic error
• Executing a break statement in the body
of a loop immediately terminates the loop
• Executing a continue statement in the
body of a loop skips to the next iteration
Assignment
• Explain with an Example the usage of For
Loop in C++
• What is the difference between While loop
and Do While Loop.

More Related Content

What's hot

Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...
Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...
Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...Jonathan Salwan
 
Instruction pipelining
Instruction pipeliningInstruction pipelining
Instruction pipeliningTech_MX
 
TMPA-2017: Simple Type Based Alias Analysis for a VLIW Processor
TMPA-2017: Simple Type Based Alias Analysis for a VLIW ProcessorTMPA-2017: Simple Type Based Alias Analysis for a VLIW Processor
TMPA-2017: Simple Type Based Alias Analysis for a VLIW ProcessorIosif Itkin
 
Presentation slides for "A formal foundation for trace-based JIT compilation"
Presentation slides for "A formal foundation for trace-based JIT compilation"Presentation slides for "A formal foundation for trace-based JIT compilation"
Presentation slides for "A formal foundation for trace-based JIT compilation"mvdcamme
 
Pipelinig hazardous
Pipelinig hazardousPipelinig hazardous
Pipelinig hazardousjasscheema
 
St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...
St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...
St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...Jonathan Salwan
 

What's hot (11)

3DD 1e Linux
3DD 1e Linux3DD 1e Linux
3DD 1e Linux
 
Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...
Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...
Sstic 2015 detailed_version_triton_concolic_execution_frame_work_f_saudel_jsa...
 
FIFOPt
FIFOPtFIFOPt
FIFOPt
 
Instruction pipelining
Instruction pipeliningInstruction pipelining
Instruction pipelining
 
TMPA-2017: Simple Type Based Alias Analysis for a VLIW Processor
TMPA-2017: Simple Type Based Alias Analysis for a VLIW ProcessorTMPA-2017: Simple Type Based Alias Analysis for a VLIW Processor
TMPA-2017: Simple Type Based Alias Analysis for a VLIW Processor
 
Presentation slides for "A formal foundation for trace-based JIT compilation"
Presentation slides for "A formal foundation for trace-based JIT compilation"Presentation slides for "A formal foundation for trace-based JIT compilation"
Presentation slides for "A formal foundation for trace-based JIT compilation"
 
Lecture03
Lecture03Lecture03
Lecture03
 
CFD - OpenFOAM
CFD - OpenFOAMCFD - OpenFOAM
CFD - OpenFOAM
 
Pipelinig hazardous
Pipelinig hazardousPipelinig hazardous
Pipelinig hazardous
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...
St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...
St hack2015 dynamic_behavior_analysis_using_binary_instrumentation_jonathan_s...
 

Similar to Object oriented programming18 control structures looping

Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetitionOnline
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptxsujatha629799
 
Control structures ii
Control structures ii Control structures ii
Control structures ii Ahmad Idrees
 
Object oriented programming17 control structures repetition statements
Object oriented programming17 control structures repetition statementsObject oriented programming17 control structures repetition statements
Object oriented programming17 control structures repetition statementsVaibhav Khanna
 
Break, continue and return
Break, continue and return Break, continue and return
Break, continue and return Jadavsejal
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesJason J Pulikkottil
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loopsManzoor ALam
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorialpinck2380
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorialpinck200
 
Control structures.ppt
Control structures.pptControl structures.ppt
Control structures.pptRahul Borate
 
Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loopsmcollison
 
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 statementsharsh kothari
 
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
 

Similar to Object oriented programming18 control structures looping (20)

Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
chapter 6.pptx
chapter 6.pptxchapter 6.pptx
chapter 6.pptx
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptx
 
Slide 6_Control Structures.pdf
Slide 6_Control Structures.pdfSlide 6_Control Structures.pdf
Slide 6_Control Structures.pdf
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Iteration
IterationIteration
Iteration
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Object oriented programming17 control structures repetition statements
Object oriented programming17 control structures repetition statementsObject oriented programming17 control structures repetition statements
Object oriented programming17 control structures repetition statements
 
Ch05
Ch05Ch05
Ch05
 
8 statement level
8 statement level8 statement level
8 statement level
 
Break, continue and return
Break, continue and return Break, continue and return
Break, continue and return
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniques
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorial
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorial
 
Control structures.ppt
Control structures.pptControl structures.ppt
Control structures.ppt
 
Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loops
 
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
 
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
 

More from Vaibhav Khanna

Information and network security 47 authentication applications
Information and network security 47 authentication applicationsInformation and network security 47 authentication applications
Information and network security 47 authentication applicationsVaibhav Khanna
 
Information and network security 46 digital signature algorithm
Information and network security 46 digital signature algorithmInformation and network security 46 digital signature algorithm
Information and network security 46 digital signature algorithmVaibhav Khanna
 
Information and network security 45 digital signature standard
Information and network security 45 digital signature standardInformation and network security 45 digital signature standard
Information and network security 45 digital signature standardVaibhav Khanna
 
Information and network security 44 direct digital signatures
Information and network security 44 direct digital signaturesInformation and network security 44 direct digital signatures
Information and network security 44 direct digital signaturesVaibhav Khanna
 
Information and network security 43 digital signatures
Information and network security 43 digital signaturesInformation and network security 43 digital signatures
Information and network security 43 digital signaturesVaibhav Khanna
 
Information and network security 42 security of message authentication code
Information and network security 42 security of message authentication codeInformation and network security 42 security of message authentication code
Information and network security 42 security of message authentication codeVaibhav Khanna
 
Information and network security 41 message authentication code
Information and network security 41 message authentication codeInformation and network security 41 message authentication code
Information and network security 41 message authentication codeVaibhav Khanna
 
Information and network security 40 sha3 secure hash algorithm
Information and network security 40 sha3 secure hash algorithmInformation and network security 40 sha3 secure hash algorithm
Information and network security 40 sha3 secure hash algorithmVaibhav Khanna
 
Information and network security 39 secure hash algorithm
Information and network security 39 secure hash algorithmInformation and network security 39 secure hash algorithm
Information and network security 39 secure hash algorithmVaibhav Khanna
 
Information and network security 38 birthday attacks and security of hash fun...
Information and network security 38 birthday attacks and security of hash fun...Information and network security 38 birthday attacks and security of hash fun...
Information and network security 38 birthday attacks and security of hash fun...Vaibhav Khanna
 
Information and network security 37 hash functions and message authentication
Information and network security 37 hash functions and message authenticationInformation and network security 37 hash functions and message authentication
Information and network security 37 hash functions and message authenticationVaibhav Khanna
 
Information and network security 35 the chinese remainder theorem
Information and network security 35 the chinese remainder theoremInformation and network security 35 the chinese remainder theorem
Information and network security 35 the chinese remainder theoremVaibhav Khanna
 
Information and network security 34 primality
Information and network security 34 primalityInformation and network security 34 primality
Information and network security 34 primalityVaibhav Khanna
 
Information and network security 33 rsa algorithm
Information and network security 33 rsa algorithmInformation and network security 33 rsa algorithm
Information and network security 33 rsa algorithmVaibhav Khanna
 
Information and network security 32 principles of public key cryptosystems
Information and network security 32 principles of public key cryptosystemsInformation and network security 32 principles of public key cryptosystems
Information and network security 32 principles of public key cryptosystemsVaibhav Khanna
 
Information and network security 31 public key cryptography
Information and network security 31 public key cryptographyInformation and network security 31 public key cryptography
Information and network security 31 public key cryptographyVaibhav Khanna
 
Information and network security 30 random numbers
Information and network security 30 random numbersInformation and network security 30 random numbers
Information and network security 30 random numbersVaibhav Khanna
 
Information and network security 29 international data encryption algorithm
Information and network security 29 international data encryption algorithmInformation and network security 29 international data encryption algorithm
Information and network security 29 international data encryption algorithmVaibhav Khanna
 
Information and network security 28 blowfish
Information and network security 28 blowfishInformation and network security 28 blowfish
Information and network security 28 blowfishVaibhav Khanna
 
Information and network security 27 triple des
Information and network security 27 triple desInformation and network security 27 triple des
Information and network security 27 triple desVaibhav Khanna
 

More from Vaibhav Khanna (20)

Information and network security 47 authentication applications
Information and network security 47 authentication applicationsInformation and network security 47 authentication applications
Information and network security 47 authentication applications
 
Information and network security 46 digital signature algorithm
Information and network security 46 digital signature algorithmInformation and network security 46 digital signature algorithm
Information and network security 46 digital signature algorithm
 
Information and network security 45 digital signature standard
Information and network security 45 digital signature standardInformation and network security 45 digital signature standard
Information and network security 45 digital signature standard
 
Information and network security 44 direct digital signatures
Information and network security 44 direct digital signaturesInformation and network security 44 direct digital signatures
Information and network security 44 direct digital signatures
 
Information and network security 43 digital signatures
Information and network security 43 digital signaturesInformation and network security 43 digital signatures
Information and network security 43 digital signatures
 
Information and network security 42 security of message authentication code
Information and network security 42 security of message authentication codeInformation and network security 42 security of message authentication code
Information and network security 42 security of message authentication code
 
Information and network security 41 message authentication code
Information and network security 41 message authentication codeInformation and network security 41 message authentication code
Information and network security 41 message authentication code
 
Information and network security 40 sha3 secure hash algorithm
Information and network security 40 sha3 secure hash algorithmInformation and network security 40 sha3 secure hash algorithm
Information and network security 40 sha3 secure hash algorithm
 
Information and network security 39 secure hash algorithm
Information and network security 39 secure hash algorithmInformation and network security 39 secure hash algorithm
Information and network security 39 secure hash algorithm
 
Information and network security 38 birthday attacks and security of hash fun...
Information and network security 38 birthday attacks and security of hash fun...Information and network security 38 birthday attacks and security of hash fun...
Information and network security 38 birthday attacks and security of hash fun...
 
Information and network security 37 hash functions and message authentication
Information and network security 37 hash functions and message authenticationInformation and network security 37 hash functions and message authentication
Information and network security 37 hash functions and message authentication
 
Information and network security 35 the chinese remainder theorem
Information and network security 35 the chinese remainder theoremInformation and network security 35 the chinese remainder theorem
Information and network security 35 the chinese remainder theorem
 
Information and network security 34 primality
Information and network security 34 primalityInformation and network security 34 primality
Information and network security 34 primality
 
Information and network security 33 rsa algorithm
Information and network security 33 rsa algorithmInformation and network security 33 rsa algorithm
Information and network security 33 rsa algorithm
 
Information and network security 32 principles of public key cryptosystems
Information and network security 32 principles of public key cryptosystemsInformation and network security 32 principles of public key cryptosystems
Information and network security 32 principles of public key cryptosystems
 
Information and network security 31 public key cryptography
Information and network security 31 public key cryptographyInformation and network security 31 public key cryptography
Information and network security 31 public key cryptography
 
Information and network security 30 random numbers
Information and network security 30 random numbersInformation and network security 30 random numbers
Information and network security 30 random numbers
 
Information and network security 29 international data encryption algorithm
Information and network security 29 international data encryption algorithmInformation and network security 29 international data encryption algorithm
Information and network security 29 international data encryption algorithm
 
Information and network security 28 blowfish
Information and network security 28 blowfishInformation and network security 28 blowfish
Information and network security 28 blowfish
 
Information and network security 27 triple des
Information and network security 27 triple desInformation and network security 27 triple des
Information and network security 27 triple des
 

Recently uploaded

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 ...harshavardhanraghave
 
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...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
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 ApplicationsAlberto González Trastoy
 
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.docxComplianceQuest1
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
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.comFatema Valibhai
 
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.jsAndolasoft Inc
 
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.pdfkalichargn70th171
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
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.pdfkalichargn70th171
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
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 ...OnePlan Solutions
 
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 🔝✔️✔️Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 

Recently uploaded (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...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
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
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.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
 
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
 
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
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
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
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
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 ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
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 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 

Object oriented programming18 control structures looping

  • 1. Object Oriented Programming: 18 Control Structures Looping Prof Neeraj Bhargava Vaibhav Khanna Department of Computer Science School of Engineering and Systems Sciences Maharshi Dayanand Saraswati University Ajmer
  • 2. 2 for Looping (Repetition) Structure • The general form of the for statement is: • The initial statement, loop condition, and update statement are called for loop control statements – initial statement usually initializes a variable (called the for loop control, or for indexed, variable) • In C++, for is a reserved word
  • 3. 3 for Looping (Repetition) Structure (continued)
  • 4. 4 for Looping (Repetition) Structure (continued)
  • 5. 5 for Looping (Repetition) Structure (continued) • C++ allows you to use fractional values for loop control variables of the double type – Results may differ • The following is a semantic error: • The following is a legal for loop: for (;;) cout << "Hello" << endl;
  • 6. 6 for Looping (Repetition) Structure (continued)
  • 7. 7 do…while Looping (Repetition) Structure • General form of a do...while: • The statement executes first, and then the expression is evaluated • To avoid an infinite loop, body must contain a statement that makes the expression false • The statement can be simple or compound • Loop always iterates at least once
  • 10.
  • 12. 12 Choosing the Right Looping Structure • All three loops have their place in C++ – If you know or can determine in advance the number of repetitions needed, the for loop is the correct choice – If you do not know and cannot determine in advance the number of repetitions needed, and it could be zero, use a while loop – If you do not know and cannot determine in advance the number of repetitions needed, and it is at least one, use a do...while loop
  • 13. 13 break and continue Statements • break and continue alter the flow of control • break statement is used for two purposes: – To exit early from a loop • Can eliminate the use of certain (flag) variables – To skip the remainder of the switch structure • After the break statement executes, the program continues with the first statement after the structure
  • 14. 14 break & continue Statements (continued) • continue is used in while, for, and do…while structures • When executed in a loop – It skips remaining statements and proceeds with the next iteration of the loop
  • 15. 15 Nested Control Structures • To create the following pattern: * ** *** **** ***** • We can use the following code: for (i = 1; i <= 5 ; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; }
  • 16. 16 Nested Control Structures (continued) • What is the result if we replace the first for statement with the following? for (i = 5; i >= 1; i--) • Answer: ***** **** *** ** *
  • 17. 17 Summary • C++ has three looping (repetition) structures: – while, for, and do…while • while, for, and do are reserved words • while and for loops are called pretest loops • do...while loop is called a posttest loop • while and for may not execute at all, but do...while always executes at least once
  • 18. 18 Summary (continued) • while: expression is the decision maker, and the statement is the body of the loop • A while loop can be: – Counter-controlled – Sentinel-controlled – EOF-controlled • In the Windows console environment, the end-of-file marker is entered using Ctrl+z
  • 19. 19 Summary (continued) • for loop: simplifies the writing of a counter-controlled while loop – Putting a semicolon at the end of the for loop is a semantic error • Executing a break statement in the body of a loop immediately terminates the loop • Executing a continue statement in the body of a loop skips to the next iteration
  • 20. Assignment • Explain with an Example the usage of For Loop in C++ • What is the difference between While loop and Do While Loop.