Download presentation

Presentation is loading. Please wait.

Published byMalia Washburne Modified over 2 years ago

1
CSC 107 – Programming For Science

2
Todays 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

3
Computer Professor MemoryHuge (> 250GB)Cant remember 19 names Computing speed Fast (> 2 billion/second)Slow (needs fingers & toes) Takes direction Does exactly as toldLeaves toilet seat up Speed of updates Nearly instantaneousWears 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

4
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

5
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

6
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 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

7
Code Structures in Programming Sequence Decision Repetition true false true false

8
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

9
Loop Structure

10
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

11
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

12
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

13
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…

14
and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and

15
over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over

16
and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and 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 EXACTLY Remember: computers do EXACTLY what they're told Have infinite patience with the stupid tasks we ask

17
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

18
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

19
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;

20
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;

21
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;

22
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

23
do-while Loop

24
Tracing do - while Loop int mPerMi = 1; float mph; do { mph=(1.0/mPerMi)*60.0; cout 1) ; cout << "Now I know." << endl;

25
Your Turn Get in groups & work on following activity

26
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!

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google