L07_while_loops

# L07_while_loops - Loops EECS 183 University of Michigan...

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

EECS 183 University of Michigan Lecture 07 Mary Lou Dorf Loops Loops

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

View Full Document
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf We Often Want to Repeat Things We Often Want to Repeat Things Take 10 steps forward… Drive West until you reach the Mississippi River… Sleep until the alarm clock goes off Great for “counting”
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf Loop Types Loop Types Loops can be controlled by counters (take 10 steps) or conditions (drive west until you reach the Mississippi river). The easiest two loops in C++ are while loop do-while loop Both are controlled by conditionals but we can set them up to count.

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

View Full Document
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf While Loop While Loop •S y n t a x The conditional can be any conditional you might use in an if statement. As with if statements you may have more than 1 statement in the loop but you need to use { }’s while (condition) statement;
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf A Simple Counting Loop A Simple Counting Loop #include <iostream> using namespace std; int main ( ) { int currentCount = 1; while (currentCount < 10) { cout << currentCount << endl; } return 0; } This is a big error – we haven’t changed currentCount in the loop – it means we can end up with an infinite loop!

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

View Full Document
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf A Simple and Correct Loop A Simple and Correct Loop #include <iostream> using namespace std; int main ( ) { int currentCount = 1; while (currentCount < 10) { cout << currentCount << endl; currentCount = currentCount + 1; } return 0; }
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf You Can Go Both Ways You Can Go Both Ways #include <iostream> using namespace std; // Countdown program int main ( ) { int currentCount = 10; while (currentCount >= 0) { cout << currentCount << endl; currentCount = currentCount – 1; } return 0; }

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

View Full Document
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf Common Errors Common Errors Off by one It’s easy to get the test or condition wrong and go either one less time or one more time through the loop than you planned.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/12/2010 for the course EECS 183 taught by Professor Dorf during the Fall '08 term at University of Michigan.

### Page1 / 29

L07_while_loops - Loops EECS 183 University of Michigan...

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

View Full Document
Ask a homework question - tutors are online