05 - Iteration and Conditionals

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

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

1 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… 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… 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 So far we have only used sequence. Sequence allows for the computation of some complicated formulas, but not the creation of dynamic programs. Iteration allows the computer program to employ repetition to achieve a goal. Iteration often allows us to devise more general solutions to problems. while statement Iteration can be accomplished with a while statement in the form while (condition) loop_body The condition evaluates to True or False. The loop body is an expression or compound statement in {braces}. If the condition is true the loop body executes, then the condition is checked again. The loop body is repeatedly executed as long as the condition holds.

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

View Full Document
2 Example: Combinatorics How many ways can you choose 5 balls in groups of 3? 123 124 125 134 135 145 234 235 245 345 10 ways! Example: Combinatorics In general if you are choosing from m things n at a time: So choosing from 5 things 3 at a time Example: Combinatorics To compute this We need to compute the factorial function. Write a function to compute N! 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; }
3 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 (1 <= 5){ 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 (True){ 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

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.

## 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 / 11

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

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

View Full Document
Ask a homework question - tutors are online