Repetition
-
Function Stubs
-
Random Numbers
-
Switching values using temporary variables
-
Repetition
-
While
-
For statement
-
Increment and Decrement
-
Do While
-
Grade program
Repetition
Function Stubs
-
Function header, prototype and call
-
No real code in the function body
-
Allows testing of main without writing all code
-
Skeleton of function
-
Message in function body to let you know you were there
Repetition
Random Numbers
-
Random number generator
-
stdlib.h header
-
srand and rand
Repetition
Switching values using temporary variables
-
To switch firstNum to secondNum
-
temp = 0;
-
temp = firstNum;
-
firstNum = secondNum;
-
secondNum = temp;
-
see page 255
Repetition
Repetition
-
Rob the mechanical man
-
Repeating one or more instructions until a condition is met
-
Brownies
-
Paying bills
-
Other names - looping, iteration
-
Repeat a specified number of times or until condition is met
-
while, do while, for
Repetition
While
-
pseudocode
-
Characteristics of the While
-
Flowcharts
-
Coding C++ while loops
-
Examples
-
Using Counters and Accumulators
-
Position of statements
-
Problems
-
Nested Repetition
Repetition
pseudocode
-
while (money in acct > 0 or no more bills)
-
repeat while (numStudents <30 )
Repetition
while (money in acct > 0 or no more bills)
-
write check
-
subtract check amount from balance
-
subtract one from bills to pay
-
end while
-
What happens if we forget to subtract bills to pay or money?
Repetition
repeat while (numStudents <30 )
-
register
-
add 1 to numStudents
-
end
Repetition
Characteristics of the While
-
Loop body
-
Condition
-
How does it process?
Repetition
Loop body
-
One statement
-
Block of statements
Repetition
Condition
-
Evaluates to true or false
-
Boolean expression
-
Where does processing go if statement is false?
Repetition
How does it process?
-
Evaluates conditions before executing loop body
-
Pre-test
-
May never execute loop body depending on condition
-
Top driven loop
-
After each execution of loop body, condition is re-evaluated
Repetition
Flowcharts
-
Diamond symbol
-
2 flowlines leaving
-
2 flowlines coming in
Repetition
Coding C++ while loops
-
Page 269 - syntax and examples
-
while (loop condition)
-
one or statements or block
-
// end while comment
-
No semicolon at end of while
-
Most times will use a block
-
Good to get in habit of {} even with one statement
-
Easier to modify
-
Will the instructions in the loop body always execute?
Repetition
Examples
-
LaProg01 - what is happening?
-
What happens if condition is initially false?
-
LaProg02 - Game - what is happening?
Repetition
Using Counters and Accumulators
-
Counter - counting something
-
Accumulator - adding values together, accumulating a total
-
Updating - changing values of accumulator or counter
Repetition
Position of statements
-
Priming read, lead read - before entering loop
-
Another read within loop
-
Values to end loops - sentinel values, trip values, trailer values
-
Makes a difference where initial values are, where increments take place
-
LaProg03
-
Calculate number of sales numSales = numSales + 1
-
Calculate total sales sumSales = sumSales + sales
Repetition
Problems
-
Endless Loops
-
Divide by Zero
Repetition
Endless Loops
-
What is it?
-
CTRL c (press both at same time) to end endless loop
-
Close DOS window
-
What if never ask for new values in a loop and are testing the value?
Repetition
Divide by Zero
-
Not allowed
-
Try with Sales Express program
-
Number of sales never gets updated, never enters loop
-
How to correct problem with divide by zero?
-
Why do we increment number sales and total before request new values?
Repetition
Nested Repetition
-
One loop inside another loop
-
Inner loop, outer loop
-
LaProg04 -- collecting sales from two regions
-
Loop for sales, Loop for Region 1 and Region 2
Repetition
For statement
-
Counter controlled loop
-
Accomplishes same as while, but more efficient coding
-
for (short =1; count <+ 3; count="count" + 1)
-
*****
-
// end for
-
for (type initial value; condition; update counter)
-
Statement or block
-
// end
-
Lbprog01 - what is happening?
-
Lbprog02 - replace a while statement with a for statement
Repetition
Increment and Decrement
-
Short hand
-
count = count + 1;
-
count++;
-
count = count - 1;
-
count--;
Repetition
Do While
-
do
-
{}
-
while (condition);
-
Post test (where while is a pre test)
-
Tests condition after completing loop first time
-
Always executes a loop at least once
-
Laprog03 - what is happening?
Maintained
by Pat Hathaway
Instructor, Information Systems Technology
Paul D. Camp Community College
Pages last updated on Tuesday, October 13, 1998.