csc 107 – programming for science. todays goal after today, should know why we use loops how to...
TRANSCRIPT
![Page 1: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/1.jpg)
LECTURE 12:WHILE & DO-WHILE STATEMENTS
CSC 107 – Programming For Science
![Page 2: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/2.jpg)
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
![Page 3: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/3.jpg)
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
![Page 4: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/4.jpg)
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
![Page 5: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/5.jpg)
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
![Page 6: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/6.jpg)
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
![Page 7: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/7.jpg)
Code Structures in Programming Sequence
Decision
Repetition
truefalse
true
false
![Page 8: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/8.jpg)
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
![Page 9: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/9.jpg)
Loop Structure
2 parts to every loop in any program:
LOOP BODY &
LOOP TERMINATION CONDITION
![Page 10: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/10.jpg)
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
![Page 11: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/11.jpg)
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
![Page 12: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/12.jpg)
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
![Page 13: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/13.jpg)
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…
![Page 14: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/14.jpg)
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
![Page 15: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/15.jpg)
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
![Page 16: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/16.jpg)
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
![Page 17: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/17.jpg)
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
![Page 18: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/18.jpg)
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
![Page 19: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/19.jpg)
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;
![Page 20: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/20.jpg)
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;
![Page 21: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/21.jpg)
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;
![Page 22: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/22.jpg)
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
![Page 23: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/23.jpg)
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
![Page 24: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/24.jpg)
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;
![Page 25: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/25.jpg)
Your Turn
Get in groups & work on following activity
![Page 26: CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when](https://reader035.vdocuments.net/reader035/viewer/2022062619/5516381455034694308b630b/html5/thumbnails/26.jpg)
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!