CPS 6, Ramm - Spring 2000 - 2/7/00 #9
Chapt 5. Iteration with Programs and Classes
- The Fence Post Problem
-
for Loop Syntax
- for(initialization; test expression; update)
- {
- statement list
- }
- Use where number of iterations is "known"
- Good for most examples shown previously
- Class: Modify
blastoff.cpp
- Problem: Adder Program
- Add an unknown number of numbers
- When to quit
- Sentinel Value
-
while
or for ?
adder.cpp
- Defining Constants
const type identifier = value;
all caps
- Pre-tested and Post-tested Loops
- Iteration (post-tested)
- initialization
- loop body
- update
- loop test
-
do-while Loop Syntax
- do
- {
- statement list
- } while ( test expression )
- Using the
do-while Loop
addwhile.cpp
- Infinite Loops
-
break Statement
- Nested Loops
multable.cpp
nesting can be hidden by using function
- Scope of Variables
- definition
- braces { }
- Private Variables
- Global Variables (XXX)
- Random Numbers & N-sided Dice:
Simulations
- Class:
dice
- Using dice
roll.cpp
- Class Diagram
- Public
- constructor
- member functions
- Private
- Constructors
- The Dice Interface
dice.h
"Declaration" not code (not Definition)
- Dice Implementation
dice.cpp
- Iteration (pre-tested)
- initialization
- loop test
- loop body
- update
- Dice/While Example
roll300.cpp
modify to use for loop