Download presentation
Presentation is loading. Please wait.
Published byGiles Spencer Modified over 9 years ago
1
PHY 107 – Programming For Science
2
Today’s Goal 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 Explain how for, while & do / while loops differ Convert between the 3 loops with same end result Explain why we would never convert between loops
3
Code Structures in Programming Sequence Decision Repetition true false true false
4
Loop Structure
5
Loop Termination Condition Most important detail of a loop This is what tells computer when loop should execute Errors common in loop termination condition Computers are dumb & happily execute infinite loops 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
6
Loop Body Actions loop performs each repetition Loop body completed before checking condition Even Even if variable reassigned no rechecking within body May contain any code including another loop Nested loops common in science & engineering 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
7
for Loop Structure
8
for Loop
9
for Loop Strengths for loops good at bounding times loop is run (Optional) Declare & initialize variable doing counts Loop statement also clearly shows loop termination (Optional) Variable‘s update made quite clear May not always have a variable controlling update Might want to execute only while some expression true Update within loop body and not just at the end Not just a single variable, but multiple variables needed
10
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
11
Tracing while Loop int mPerMi = 1; float mph; while (mPerMi > 1) { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } printf("Now I know.\n“);
12
Tracing while Loop int mPerMi = 1; float mph; while (mPerMi >= 1) { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } printf("Now I know.\n“);
13
Tracing while Loop int mPerMi = 1; float mph; while ((mPerMi >= 1) && (mPerMi < 4)) { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } printf("Now I know.\n“);
14
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
15
do - while Loop
16
Sample do - while Loop int mPerMi = 1; float mph; do { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } while (mPerMi > 1) ;
17
Your Turn Get in groups & work on following activity
18
For Next Lecture
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.