5. Iteration and Conditionals

# 5. Iteration and Conditionals - Iteration and Conditionals...

This preview shows pages 1–5. Sign up to view the full content.

M. Wellman 1 Iteration and Conditionals ENGR 101, Lecture 5: 22 Sep 10 Announcements Project 2 due tonight (Wed 9pm) Exam #1 Tue 5 Oct if you have a potential conflict, contact Nader Jawad (njawad@umich.edu) Classroom evacuation routes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
M. Wellman 2 Fundamental Algorithmic Control Constructs Sequence Selection Iteration Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder( a , c ) 0 OR remainder( b , c ) 0) 6: subtract one from c 7: return c 8: end Sequence Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while ( remainder( a , c ) 0 OR remainder( b , c ) 0) 6: subtract one from c 7: return c 8: end Algorithm steps are executed in a specified linear order. In this case, order is implicit in the textual order on screen C++ sequence constructs Compound statements Operator precedence rules for expression evaluation Function and procedure invocation
M. Wellman 3 Iteration Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder( a , c ) 0 OR remainder( b , c ) 0) 6: subtract one from c 7: return c 8: end Repetitive execution of an algorithm element, as long as specified condition obtains Repetition Perform a similar computational step many times in order to achieve a goal. In greatest common factor repetition, value of variable c is different on each iteration Number of repetitions may be indefinite, dependent on problem and computation progress. Iteration often allows us to devise more general solutions to problems. while (remainder( a , c ) 0 OR remainder( b , c ) 0) subtract one from c

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
M. Wellman 4 Example: Calculate Weeks to Produce PCs How many PCs can we make this week? • Decrement PCs
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/25/2011 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

### Page1 / 18

5. Iteration and Conditionals - Iteration and Conditionals...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online