Lecture 13: Loops
Monday, March 12, 2012[Up] [Previous Lecture] [Next Lecture]
Reading Assigned: 5.1 - 5.3
Homework Due: None
Homework Assigned: Homework 06 -- assigned last class
Classwork Assigned: None
Topics Covered:
- NEW Homework 6 questions
- Quiz questions review
- Loops
- Our current constructs do not allow us to go "back" in our code
- Functions allow repeating code, but only a fixed number of times
- Loops allows us to dynamically control how many times code will run
- Three main types of loops in C: while, do-while, and for (for will be covered next class)
- while
- While loops are made up of while keyword, an expression to test, and code to execute
- in general, while(test) { code; }
- Example of using while loop with commentary
- do ... while
- A while loop tests it condition prior to the loop starting
- A do-while loop will always execute the loop once
- The do-while test condition is only tested at the end of the loop
- After first time through the code, a do-while loop is identical to a while loop in functionality
- in general, do { code; } while(test);
- Example of using do-while loop