csc 107 – programming for science. todays goal after today, should know why we use loops how to...

26
LECTURE 12: WHILE & DO-WHILE STATEMENTS CSC 107 – Programming For Science

Upload: malia-washburne

Post on 28-Mar-2015

217 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and 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

over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over 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

and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and 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

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

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

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

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

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

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

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

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

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

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!