So, two of the programs produced the same output for the method call of loops(2). How do these results compare to your predictions? Here is a screenshot showing these outputs. What do you think the output of each loop would be? How do you know? Take a minute to predict the output for each version of the loops() program if it was called with a method call of loops(2). Imagine that we executed each of the three programs shown above with a method call of loops(2). Which of these differences are superficial, and which differences are substantive? What are the ramifications of these differences? Analyzing loop outputs We might notice that the programs check similar conditions but in different ways: the For loop increments from 1 to n the While loop checks to see if i ≤ n (in order to decide if the loop should run again) and, the Do While loop checks to see if i > n (in order to decide if the loop should break and not run again). (Note that here we are using a While loop to model a For loop and another While loop to model a Do While loop. We might also notice that the loop variable i is explicitly incremented within the body of each of the loops (with statements of i=i+1). We might notice that the loop variable i is explicitly initialized before the While and Do While loops, but that this initialization is included in the For loop statement. What other similarities do you notice? What differences do you notice across these programs? (Recall that i**2 is Python for i2 and that you need to include from math import * to use this command.) Each loop includes the same action within it of print(i,i2). We might notice that each program uses a formal parameter of n and a local loop variable of i. What similarities do you notice across these programs? These three programs are designed to model a For loop, a While loop and a Do While loop respectively. ![]() ![]() How are they similar? How are they different?Ĭomparing and contrasting loop structuresĬonsider the three programs shown in the images below. Now, we will step back and consider these three iterative control structures in relation to each other. Through the last three activities in the workshop, we have explored each loop structure in isolation. How do the three loop structures prevalent in computer science languages ( For, While, and Do While loops) compare to and contrast with each other? What are the implications of these similarities and differences?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |