Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 - Programming for Science Lecture 12: while & do/while Loops.

Similar presentations


Presentation on theme: "CSC 107 - Programming for Science Lecture 12: while & do/while Loops."— Presentation transcript:

1 CSC 107 - Programming for Science Lecture 12: while & do/while Loops

2 Today’s Goal After today, should know why we use loops and how to use while and do/while loops work  Know when use of either loop is preferable  Understand loop termination conditions  Write bounded and unbounded loops

3 Program Structure All code so far has been linear  Runs through code in nearly sequential order  Perform actions at most once  Ignores the main benefit of computers Computers are fast, but dumb  (NO drawing parallels to any person live or dead)  Best performing simple * tasks over-and-over  Repetitive, not sequential, execution

4 Loops Direct actions to be performed repetitively  Over 90% computer execution spent in loops Scientific & artistic programming loop-based  Simulate how system changes over time  Compute what each pixel on screen should contain  Block-by-block evaluation of sea waves  Analyzing traffic patterns throughout day All loops can be broken into two parts  Loop body  Loop termination/loop continuation condition

5 Loop termination/continuation Most important detail of a loop  Specifies when loop should execute Errors in loop termination very common  Infinite loop continues like scratched CD  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 over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and …

6  over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over over and over and over and over and over and over and … Waste a lot of time waiting for these programs to finish

7 Loop Body Actions loop performs each repetition Can very simple  PowerPoint blinking cursor waiting for me to type Can be very complex  Simulating U.S. weather for that moment Errors here can be hard to fix  Executed many times  Error can spread as loop repeats  First test on paper & verify it works  Check that small errors cannot grow with repetition

8 while Loop while (expression) { statement;... } Evaluates the “boolean” expression If “true”, execute entire loop body  After loop body completes, check expression Remains in loop while expression is “true”

9 Tracing while Loop 0 int mPerMi = 1; 1 float mph; 2 while (mPerMi > 1) { 3 mph=(1.0/mPerM)*60.0; 4 printf(“%d min/mile”, mPerMi); 5 printf(“ = %lf mph\n”, mph); 6 mPerMi += 1; 7 } 8 printf(“Now I know\n”); Line #mPerMimph

10 Tracing while Loop 0 int mPerMi = 1; 1 float mph; 2 while (mPerMi > 0) { 3 mph=(1.0/mPerM)*60.0; 4 printf(“%d min/mile”, mPerMi); 5 printf(“ = %lf mph\n”, mph); 6 mPerMi += 1; 7 } 8 printf(“Now I know\n”); Line #mPerMimph

11 Tracing while Loop 0 int mPerMi = 1; 1 float mph; 2 while (mPerMi > 0) { 3 mph=(1.0/mPerM)*60.0; 4 printf(“%d min/mile”, mPerMi); 5 printf(“ = %lf mph\n”, mph); 6 mPerMi -= 1; 7 } 8 printf(“Now I know\n”); Line #mPerMimph

12 Sometimes you feel like a nut… Sometimes should execute loop only if loop continuation expression was “true”  while performs check before executing loop Sometimes want to execute loop body at least once  do-while checks loop continuation expression after each execution of loop Only difference between while & do- while

13 do-while Loop do { statement;... } while (expression); Execute entire loop body  Then evaluate the “boolean” expression If “true”, re-execute entire loop body  After loop body, recheck expression Unlike all other expressions, semicolon needed

14 Your Turn Get into groups and complete daily activity

15 For Next Lecture Keep reading Sections 3.4 of book  Do not need to understand all the details  But important knowing what is not understood Start week #5 weekly assignment Finish lab #3 First programming assignment on web  Due 1 week from Friday


Download ppt "CSC 107 - Programming for Science Lecture 12: while & do/while Loops."

Similar presentations


Ads by Google