L07_while_loops - Loops EECS 183 University of Michigan...

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

View Full Document Right Arrow Icon
EECS 183 University of Michigan Lecture 07 Mary Lou Dorf Loops Loops
Background image of page 1

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

View Full DocumentRight Arrow Icon
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”
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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;
Background image of page 4
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!
Background image of page 5

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 6
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; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
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.
Background image of page 8
Image of page 9
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 Right Arrow Icon
Ask a homework question - tutors are online