




























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A case study on counter-controlled repetition, a repetition structure in programming where the loop is repeated until a counter reaches a certain value. Examples in c++ and discusses the essentials of counter-controlled repetition, such as the need for a control variable, initialization, repetition condition, and increment. It also covers the use of assignment operators and break/continue statements within loops.
Typology: Slides
1 / 68
This page cannot be seen from the preview
Don't miss anything!





























































Chapter 4 - Control Structures
Introduction Algorithms Pseudocode Control Structures if Selection Structure if/else Selection Structure while Repetition Structure Formulating Algorithms: Case Study 1 (Counter- Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled Repetition for Repetition Structure Examples Using the for Structure
Solved by executing a series of actions in a specific order
Actions to be executed Order to be executed
Example: recipe
Specifies the order in which statements are executed
Artificial, informal language used to develop algorithms Similar to everyday English
Used to think out program before coding Easy to convert into C++ program Only executable statements No need to declare variables
C++ provides three types of repetition statements (also called looping statements or loops) that enable programs to perform statements repeatedly as long as a condition (called the loop-continuation condition ) remains true.
The repetition statements are the while , do ... while and for statements.
The while and for statements perform the action in their bodies zero or more times if the loop- continuation condition is initially false, the action will not execute.
The do...while statement performs the action in its body at least once.
C++ keywords (Cannot be used as variable names)
C++ Keyword s Keywords common to the C and C++ programming languages auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while C++ only keywords asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t
Choose among alternative courses of action Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” If the condition is true Print statement executed, program continues to next statement If the condition is false Print statement ignored, program continues Indenting makes programs easier to read C++ ignores whitespace characters (tabs, spaces, etc.)
If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed";
Indicates decision is to be made
Contains an expression that can be true or false Test condition, follow path
Single-entry/single-exit
Performs action if condition true
Different actions if conditions true or false
if student’s grade is greater than or equal to 60 print “Passed” else print “Failed”
if ( grade >= 60 ) cout << "Passed"; else cout << "Failed"; docsity.com
Ternary conditional operator ( ?: )
Three arguments (condition, value if true , value if false )
Code could be written:
cout << ( grade >= 60? “Passed” : “Failed” );
The following conditional expression also prints
"Passed" or "Failed":
grade >= 60? cout << "Passed" : cout <<
"Failed";
Nested if/else structures
One inside another, test for multiple cases Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”
if ( grade >= 90 ) // 90 and above cout << "A"; else if ( grade >= 80 ) // 80- cout << "B"; else if ( grade >= 70 ) // 70- cout << "C"; else if ( grade >= 60 ) // 60- cout << "D"; else // less than 60 cout << "F";
Action repeated while some condition remains true Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list while loop repeated until condition becomes false
int product = 3; while ( product <= 100 ) product = 3 * product;
product <= 100 (^) product = 3 * product
true
false