SlideShare a Scribd company logo
1 of 13
Object Oriented Programming: 17
Control Structure - Repetition
Prof Neeraj Bhargava
Vaibhav Khanna
Department of Computer Science
School of Engineering and Systems Sciences
Maharshi Dayanand Saraswati University Ajmer
2
Why Is Repetition Needed?
โ€ข Repetition allows you to efficiently use
variables
โ€ข Can input, add, and average multiple numbers
using a limited number of variables
โ€ข For example, to add five numbers:
โ€“ Declare a variable for each number, input the numbers and
add the variables together
โ€“ Create a loop that reads a number into a variable and adds
it to a variable that contains the sum of the numbers
3
while Looping (Repetition) Structure
โ€ข The general form of the while statement is:
while is a reserved word
โ€ข Statement can be simple or compound
โ€ข Expression acts as a decision maker and is
usually a logical expression
โ€ข Statement is called the body of the loop
โ€ข The parentheses are part of the syntax
4
while Looping (Repetition) Structure
(continued)
โ€ข Infinite loop: continues to execute endlessly
โ€“ Avoided by including statements in loop body that
assure exit condition is eventually false
5
while Looping (Repetition) Structure
(continued)
6
Designing while Loops
7
Case 1: Counter-Controlled while
Loops
โ€ข If you know exactly how many pieces of
data need to be read, the while loop
becomes a counter-controlled loop
8
Case 2: Sentinel-Controlled while
Loops
โ€ข Sentinel variable is tested in the condition and
loop ends when sentinel is encountered
9
Case 3: Flag-Controlled while Loops
โ€ข A flag-controlled while loop uses a bool
variable to control the loop
โ€ข The flag-controlled while loop takes the
form:
10
Case 4: EOF-Controlled while Loops
โ€ข Use an EOF (End Of File)-controlled while
loop
โ€ข The logical value returned by cin can
determine if the program has ended input
11
eof Function
โ€ข The function eof can determine the end
of file status
โ€ข Like other I/O functions (get, ignore,
peek), eof is a member of data type
istream
โ€ข The syntax for the function eof is:
where istreamVar is an input stream
variable, such as cin
12
More on Expressions in while
Statements
โ€ข The expression in a while statement can be
complex
โ€“ For example:
while ((noOfGuesses < 5) && (!isGuessed))
{
โ€ฆ
}
Assignment
โ€ข Explain the usage of while loop in C++

More Related Content

Similar to Object oriented programming17 control structures repetition statements

FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
jaychoudhary37
ย 
Week04
Week04Week04
Week04
hccit
ย 
Repetition Structures
Repetition StructuresRepetition Structures
Repetition Structures
primeteacher32
ย 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
Engr Saghir
ย 
Functional-style control flow in F#
Functional-style control flow in F#Functional-style control flow in F#
Functional-style control flow in F#
LincolnAtkinson
ย 
Von Neumann Architecture microcontroller.pptx
Von Neumann Architecture microcontroller.pptxVon Neumann Architecture microcontroller.pptx
Von Neumann Architecture microcontroller.pptx
SUNILNYATI2
ย 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
Adeel Hamid
ย 
ch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included picturesch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included pictures
PranjalRana4
ย 

Similar to Object oriented programming17 control structures repetition statements (20)

FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
ย 
Week04
Week04Week04
Week04
ย 
Repetition Structures
Repetition StructuresRepetition Structures
Repetition Structures
ย 
Ch-4.pdf
Ch-4.pdfCh-4.pdf
Ch-4.pdf
ย 
com.pptx
com.pptxcom.pptx
com.pptx
ย 
chapter 6.pptx
chapter 6.pptxchapter 6.pptx
chapter 6.pptx
ย 
Introduction.pptx
Introduction.pptxIntroduction.pptx
Introduction.pptx
ย 
Introduction to reactive programming
Introduction to reactive programmingIntroduction to reactive programming
Introduction to reactive programming
ย 
VLIW Processors
VLIW ProcessorsVLIW Processors
VLIW Processors
ย 
System Programming - Interprocess communication
System Programming - Interprocess communicationSystem Programming - Interprocess communication
System Programming - Interprocess communication
ย 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
ย 
Functional-style control flow in F#
Functional-style control flow in F#Functional-style control flow in F#
Functional-style control flow in F#
ย 
Von Neumann Architecture microcontroller.pptx
Von Neumann Architecture microcontroller.pptxVon Neumann Architecture microcontroller.pptx
Von Neumann Architecture microcontroller.pptx
ย 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
ย 
Ch05
Ch05Ch05
Ch05
ย 
Lesson 1 of c programming algorithms and flowcharts.pptx
Lesson 1 of c programming algorithms and flowcharts.pptxLesson 1 of c programming algorithms and flowcharts.pptx
Lesson 1 of c programming algorithms and flowcharts.pptx
ย 
UNIT I-Processes.pptx
UNIT I-Processes.pptxUNIT I-Processes.pptx
UNIT I-Processes.pptx
ย 
Erlang workshopdrammen
Erlang workshopdrammenErlang workshopdrammen
Erlang workshopdrammen
ย 
ch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included picturesch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included pictures
ย 
java in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariyajava in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariya
ย 

More from Vaibhav 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

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
anilsa9823
ย 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
ย 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
bodapatigopi8531
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
anilsa9823
ย 

Recently uploaded (20)

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
ย 
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...
ย 
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
ย 
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
ย 
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 ...
ย 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
ย 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
ย 
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
ย 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
ย 
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
ย 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
ย 
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
ย 
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...
ย 
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...
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
ย 
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
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
ย 
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
ย 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
ย 

Object oriented programming17 control structures repetition statements

  • 1. Object Oriented Programming: 17 Control Structure - Repetition Prof Neeraj Bhargava Vaibhav Khanna Department of Computer Science School of Engineering and Systems Sciences Maharshi Dayanand Saraswati University Ajmer
  • 2. 2 Why Is Repetition Needed? โ€ข Repetition allows you to efficiently use variables โ€ข Can input, add, and average multiple numbers using a limited number of variables โ€ข For example, to add five numbers: โ€“ Declare a variable for each number, input the numbers and add the variables together โ€“ Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers
  • 3. 3 while Looping (Repetition) Structure โ€ข The general form of the while statement is: while is a reserved word โ€ข Statement can be simple or compound โ€ข Expression acts as a decision maker and is usually a logical expression โ€ข Statement is called the body of the loop โ€ข The parentheses are part of the syntax
  • 4. 4 while Looping (Repetition) Structure (continued) โ€ข Infinite loop: continues to execute endlessly โ€“ Avoided by including statements in loop body that assure exit condition is eventually false
  • 5. 5 while Looping (Repetition) Structure (continued)
  • 7. 7 Case 1: Counter-Controlled while Loops โ€ข If you know exactly how many pieces of data need to be read, the while loop becomes a counter-controlled loop
  • 8. 8 Case 2: Sentinel-Controlled while Loops โ€ข Sentinel variable is tested in the condition and loop ends when sentinel is encountered
  • 9. 9 Case 3: Flag-Controlled while Loops โ€ข A flag-controlled while loop uses a bool variable to control the loop โ€ข The flag-controlled while loop takes the form:
  • 10. 10 Case 4: EOF-Controlled while Loops โ€ข Use an EOF (End Of File)-controlled while loop โ€ข The logical value returned by cin can determine if the program has ended input
  • 11. 11 eof Function โ€ข The function eof can determine the end of file status โ€ข Like other I/O functions (get, ignore, peek), eof is a member of data type istream โ€ข The syntax for the function eof is: where istreamVar is an input stream variable, such as cin
  • 12. 12 More on Expressions in while Statements โ€ข The expression in a while statement can be complex โ€“ For example: while ((noOfGuesses < 5) && (!isGuessed)) { โ€ฆ }
  • 13. Assignment โ€ข Explain the usage of while loop in C++