LECTURE 12:WHILE & DO-WHILE STATEMENTS
CSC 107 – Programming For Science
Today’s Goal
After today, should know why we use loops
How to use while & do/while loops in code Know & explain when use of either loop
preferable Understand loop termination conditions Be able to write bounded and unbounded
loops
Computer Professor
Memory Huge (> 250GB) Can’t remember 19 names
Computingspeed
Fast (> 2 billion/second)
Slow (needs fingers & toes)
Takes direction
Does exactly as told Leaves toilet seat up
Speed of updates
Nearly instantaneous Wears t-shirts from 1989
Ability to plan & reason
Cannot make plans; No reasoning skills;Lacks common sense
Makes (semi-)complicated plans; Good reasoning skills;Lacks common sense
Computer vs. Professor
People are very good at reasoning & analyzing Slow computing outcome of each potential
decision Considering every possible data
combinations hard Trouble when lots of data available to be
examined
People vs. Computers
Computers can perform simple tasks quickly Maintain billions of data items within
memory Very good when at performing simple
operations Cannot create plan since lacks concept of
future To do any task, must be given specific
instructions
People vs. Computers
How To Use A Computer
So far, everything has been linear Sequentially executes code (with ways to
skip steps) Each action in program executed at most
once Ignores main strengths of computers &
how to use Computers are fast but dumb
(No drawing parallels – I am not fast) Performs best repeating simple tasks
Code Structures in Programming Sequence
Decision
Repetition
truefalse
true
false
Loops
Main engine of how computers used Over 90% of execution time spent in loops
Loop-based code found in most science & art Simulate system changes over lifetime of
widget Compute how to draw each pixel on screen Block-by-block evaluation of sea waves Look at data to analyze traffic patterns
during day
Loop Structure
2 parts to every loop in any program:
LOOP BODY &
LOOP TERMINATION CONDITION
Loop Termination Condition
Most important detail of a loop This is what tells computer when loop
should execute Errors common in loop termination
condition Infinite loop continues like scratched record
Loop Termination Condition
Most important detail of a loop This is what tells computer when loop
should execute Errors common in loop termination
condition Infinite loop continues like scratched record
CD
Loop Termination Condition
Most important detail of a loop This is what tells computer when loop
should execute Errors common in loop termination
condition Infinite loop continues like broken iPod
Loop Termination Condition
Most important detail of a loop This is what tells computer when loop
should execute Errors common in loop termination
condition Infinite loop continues like broken iPod Does same thing over and over and over
and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and…
and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and
over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over
and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and
Waste lot of time waiting for this to finish Remember: computers do EXACTLY what
they're told Have infinite patience with the stupid tasks
we ask
Loop Body
Actions loop performs each repetition Often these actions can be very simple
Cursor blinking waiting for me to type in PowerPoint
In some programs, these can be very complex Simulating U.S. weather for the next 72
hours Finding and then fixing errors can be
hard Error may be small, but executed many
times… Always test out on paper to verify idea
works, first
while Loop
while (expression) { statement; ...}
Evaluates expression to find its value If true, executes entire loop body
Re-check after each pass through loop, only Continue with loop while expression
checks true
Tracing while Loop
int mPerMi = 1;float mph;while (mPerMi > 1) { mph=(1.0/mPerMi)*60.0; cout << mPerMi << "min/mile = "; mPerMi += 1; cout << mph << " mph" << endl;}cout << "Now I know." << endl;
Tracing while Loop
int mPerMi = 1;float mph;while (mPerMi >= 1) { mph=(1.0/mPerMi)*60.0; cout << mPerMi << "min/mile = "; mPerMi += 1; cout << mph << " mph" << endl;}cout << “Now I know." << endl;
Tracing while Loop
int mPerMi = 1;float mph;while ((mPerMi >= 1) && (mPerMi < 4)) { mph=(1.0/mPerMi)*60.0; cout << mPerMi << "min/mile = "; mPerMi += 1; cout << mph << " mph" << endl;}cout << “Now I know." << endl;
Sometimes you feel like a nut… May want loop only if expression is true
while checks before entering loop to see if true
But may want to execute loop body at least once do-while checks after executing of loop
Why use one and not the other? Depends on when you want check to occur Otherwise, there are not any real
differences
do-while Loop
do { statement; ... } while (expression);
First, loop body is executed Expression is checked only after loop body
executed Re-execute entire loop body, if it was
true Recheck expression after each pass through
loop Unlike all other expressions, semicolon
needed
Tracing do-while Loop
int mPerMi = 1;float mph;do { mph=(1.0/mPerMi)*60.0; cout << mPerMi << "min/mile = "; mPerMi += 1; cout << mph << " mph" << endl;} while (mPerMi > 1) ;cout << "Now I know." << endl;
Your Turn
Get in groups & work on following activity
For Next Lecture
Read sections 8.4 for Monday What is a for loop? How does it differ from a while loop? When would use use a for vs. while loop?
Week #4 weekly assignment due Tuesday If problem takes more than 10 minutes,
TALK TO ME! Project #1 due Monday
May take some time to complete, so start it soon!