05 - Iteration and Conditionals

# 05 - Iteration and Conditionals - Engineering 101 Iteration...

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

Engineering 101 Iteration and Conditionals

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

View Full Document
Quote of the Day - Martin H. Fischer It is not hard to learn more. What is hard is to unlearn  when you discover yourself wrong.
Before we get started… n A note on the increment/decrement operators: x++; x--; is the same as is the same as x = x + 1; x = x - 1; Increment Operator Decrement Operator

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

View Full Document
Before we get started… n And some more nice little shorthands: x += 5; x -= 7; is the same as is the same as x = x + 5; x = x - 7;
Iteration n So far we have only used  sequence . n Sequence  allows for the computation of  some complicated formulas, but not the  creation of dynamic programs. n Iteration  allows the computer program to  employ  repetition  to achieve a goal.  n Iteration  often allows us to devise  more  general solutions  to problems.

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

View Full Document
while  statement n Iteration  can be accomplished with a while  statement in the form while (condition) loop_body n The  condition  evaluates to True or False. n The  loop body  is an expression or compound  statement in {braces}. n If the condition is true  the loop body executes,  then the condition is checked again. n The loop body is repeatedly executed  as long as  the condition holds .
Example: Combinatorics n How many ways can you choose 5 balls in  groups of 3? 123 124 125 134 135 145 234 235 245 345 n 10 ways!

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

View Full Document
Example: Combinatorics n In general if you are choosing from m things n  at a time: n So choosing from 5 things 3 at a time ( 29 ! ! ! n m n m n m C - = = ( 29 ( 29 10 2 6 120 1 2 1 2 3 1 2 3 4 5 ! 2 ! 3 ! 5 = = = = C
Example: Combinatorics n To compute this n We need to compute the factorial function. n Write a function  to compute N! ( 29 ! ! ! n m n m n m C - = =

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

View Full Document
Factorial Function // factorial, assumes N is positive int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; } return f; } int main( ){ cout << fact(5); return 0; }
Factorial Function // factorial, assumes N is positive int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; } return f; } N 5 int main( ){ cout << fact(5); return 0; }

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

View Full Document
Factorial Function // factorial, assumes N is positive int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; } return f; } N 5 f 1 count 1 int main( ){ cout << fact(5); return 0; }
Factorial Function // factorial, assumes N is positive int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; } return f; } N 5 f 1 count 1 int main( ){ cout << fact(5); return 0; }

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

View Full Document
Factorial Function // factorial, assumes N is positive int fact( int N){
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/30/2012 for the course ENGR 101 taught by Professor Ringenberg during the Winter '07 term at University of Michigan.

### Page1 / 62

05 - Iteration and Conditionals - Engineering 101 Iteration...

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

View Full Document
Ask a homework question - tutors are online