PHY 107 – Programming For Science
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
Code Structures in Programming Sequence Decision Repetition true false true false
Loop Structure
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
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
for Loop Structure
for Loop
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
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
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“);
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“);
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“);
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
do - while Loop
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) ;
Your Turn Get in groups & work on following activity
For Next Lecture