CSCI 1300
Lecture Notes
9/18/97
Administrative Stuff
-
Repetition
-
- For Loops
- While Loops
- Do-While Loops
Administrative Stuff
Suggestion: read the programming pointers in the various chapters
Grading: I will hand back the quiz and assignment #2 on Tuesday
Review: if statements
Examples:
// Check age
if(age > 100) {
cout << "Whoa\n";
}
// Resident => low tuition
// Non-resident => high tuition
if(resident == 1) {
tuition = $2000;
cout << "Resident\n";
} else {
tuition = $10000;
cout << "Non-resident\n";
}
// :-)
if( (month == 1) && (day == 1) ) {
cout << "Happy New Year\n";
}
// f = ma;
if( m != 0 ) {
a = f/m;
} else {
a = 0;
}
// check input
if( c == 'a') {
do_something();
} else if (c == 'b') {
do_something_else();
} else {
go_home();
}
Repetition
Loops of various sorts are used to repeat a set of statements some
number of times.
-
There are three basic ways to implement loops in C++
-
- for loops
- while loops
- do-while loops
For loops
For loops are used to repeat a set of statements some specific number
of times.
They are often used for numerical computations where the number of
iterations (passes through the loop) are known when the for statement
is first executed.
Example: Summing all of the numbers from 1 to 100
The tedious, inflexible way - repeating
everything over and over
The cool way - using a for loop
The cool, flexible way - using a for loop with
a user specified limit
-
As the above examples show, for loops have 4 parts:
-
- An initialization expression
- A loop condition
- A step expression
- The body of the for loop
-
The initialization expression
-
- Sets the loop control variable to its initial value
- The loop control variable is usually, but not always, an integer
- i is the loop control variable in the above examples
- i = 0; is the initialization expression in the above
examples
-
The loop condition
-
- Compares the loop control variable with some limit value
- The limit value may be a constant (like 100) or a variable (like
count)
- The Loop condition must evaluate to either TRUE or FALSE
(remember conditions from last week)
- If the loop condition evaluates to TRUE
- - The body of the for loop is executed
- - The step expression is executed
- - The loop condition is checked again
- If the loop condition evaluates to FALSE
- - The for statement ends and the program continues with whatever is
after it
- i <= 100; and i <= count; are the loop conditions
in the above example
-
The step expression
-
- Modifies the loop control variable before the next execution of
the loop condition
- i++; in the above examples
-
The loop body
-
- Actually does the business of whatever the loop is supposed to do
each time around
- sum += 1; in the above examples
Note: A break; statement exits the loop immediately
While Loops
While loops are a little more general than for loops. In particular,
it can be used where the number of iterations of the loop is not known
beforehand.
-
While statements have 2 parts:
-
- The loop condition
- The loop body
These are just like in for loops.
If the loop condition is TRUE
The loop body is executed
The loop condition is evaluated again
If the loop condition is FALSE
The loop body is not executed
Example: echo.cc
-
How does it stop?
-
- The loop body can change something so the loop condition is FALSE
- The loop body can execute a break; statement
- Maybe it never stops
Example: cat.cc - prints a text file on the screen
Example: cat2.cc - prints a text file on the screen
Do-While Loops
Do-while loops are very much like while loops, except that the loop
body is executed once before the loop condition is ever evaluated.
After that, they are exactly the same.
Example:
do {
cout << "This is a test";
} while(0);