05 Iteration and Conditionals

05 Iteration and Conditionals - Click to edit Master...

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Engineering 101 Iteration and Conditionals 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 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 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 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! 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- = = 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; } 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; } Factorial Function // factorial, assumes N is positive int fact( int N){ int count=1, f=1;...
View Full Document

This note was uploaded on 04/07/2010 for the course ENGR engr 101 taught by Professor Rinenberg during the Spring '10 term at University of Michigan.

Page1 / 63

05 Iteration and Conditionals - Click to edit Master...

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

View Full Document
Ask a homework question - tutors are online