Docsity

Counter-Controlled Repetition: A Case Study in Algorithm Formulation, Slides of Programming for Engineers

Partial preview of the text

Download Counter-Controlled Repetition: A Case Study in Algorithm Formulation and more Slides Programming for Engineers in PDF only on Docsity!

Programming for Engineers II

(EE 112 )

Chapter 4&

Control Statements

Chapter 4 - Control Structures

Outline

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

Algorithms

 Computing problems

 Solved by executing a series of actions in a specific order

 Algorithm a procedure determining

 Actions to be executed  Order to be executed

 Example: recipe

 Program control

 Specifies the order in which statements are executed

Pseudocode

 Pseudocode

 Artificial, informal language used to develop algorithms  Similar to everyday English

 Not executed on computers

 Used to think out program before coding  Easy to convert into C++ program  Only executable statements  No need to declare variables

Repetition Statements in C++

 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

if Selection Structure

 Selection structure

 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 Selection Structure

 Translation into C++

If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed";

 Diamond symbol (decision symbol)

 Indicates decision is to be made

 Contains an expression that can be true or false  Test condition, follow path

 if structure

 Single-entry/single-exit

if/else Selection Structure

 if

 Performs action if condition true

 if/else

 Different actions if conditions true or false

 Pseudocode

if student’s grade is greater than or equal to 60 print “Passed” else print “Failed”

 C++ code

if ( grade >= 60 ) cout << "Passed"; else cout << "Failed"; docsity.com

if/else Selection Structure

 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";

if/else Selection Structure

 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/else Selection Structure

 Example

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";

while Repetition Structure

 Repetition structure

 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 listwhile loop repeated until condition becomes false

 Example

int product = 3; while ( product <= 100 ) product = 3 * product;

The while Repetition Structure

 Flowchart of while loop

product <= 100 (^) product = 3 * product

true

false