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

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

2011/2012

Uploaded on 07/25/2012

hun_i
hun_i 🇮🇳

3.7

(3)

54 documents

1 / 68

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming for Engineers II
(EE112 )
Chapter 4&5
Control Statements
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44

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