Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
184_notes:loops [2022/04/14 01:50] – woodsna1 | 184_notes:loops [2022/05/07 01:19] (current) – woodsna1 | ||
---|---|---|---|
Line 16: | Line 16: | ||
</ | </ | ||
- | That will take a long time, and programmers | + | That will take a long time to write, and physicists |
+ | < | ||
+ | i = 1 | ||
+ | while i <= 100: | ||
+ | print(i) | ||
+ | i = i + 1 | ||
+ | </ | ||
+ | |||
+ | You guessed it--in just a few lines, this code prints the numbers from 1 to 100. Let's break down what it's doing. | ||
+ | ==== Lecture Video ==== | ||
+ | |||
+ | {{youtube> | ||
+ | |||
+ | ==== While Loops ==== | ||
+ | |||
+ | Here's another example of a while loop: | ||
+ | |||
+ | < | ||
+ | # Initialize position | ||
+ | pos = vec(0, 0, 0) | ||
+ | |||
+ | # Initialize velocity | ||
+ | v = vec(1, 0, 0) | ||
+ | |||
+ | # Counter variable | ||
+ | t = 0 | ||
+ | |||
+ | # Increment amount | ||
+ | dt = 1 | ||
+ | |||
+ | # Loop! | ||
+ | while t <= 200: # Conditional | ||
+ | pos += v*dt # Operation | ||
+ | t += dt # Increment | ||
+ | </ | ||
+ | |||
+ | All of the lines before the while loop are setting up variables used in the while loop: position, velocity, time, and change in time. These lines will only run once, before the while loop starts. Everything " | ||
+ | |||
+ | The conditional is evaluated every time before the while loop runs. In the case of while loops, the conditional is whatever comes after " | ||
+ | |||
+ | Usually, some kind of counter variable (in the above example it's time) is included in the conditional of a while loop. This counter variable is adjusted within the loop, and specifies the range of values that the loop will operate over. You can think of the above loop as having bounds, similarly to an integral, from time = 0 to time = 200. | ||
+ | |||
+ | We usually call each " | ||
+ | |||
+ | ==== Nesting While Loops ==== | ||
+ | |||
+ | Think about calculating the electric potential in each square of a chess board. You could randomly choose squares to calculate until you finished the board, or work through row-by-row. Computers are good at working systematically, | ||
+ | {{ : | ||
+ | How could we use a while loop to generate the position of each square in the grid? It's tempting to do the following: | ||
+ | < | ||
+ | row = 1 | ||
+ | column = 1 | ||
+ | while row <= 8 and column <= 8: | ||
+ | # Calculate E field for (row, column) | ||
+ | row += 1 | ||
+ | column += 1 | ||
+ | </ | ||
+ | Because row and column always increase by the same amount, this will only calculate the E field for boxes (1, 1), (2, 2), (3, 3), and so on. This doesn' | ||
+ | |||
+ | Instead, we need to use a nested while loop: | ||
+ | < | ||
+ | row = 1 | ||
+ | while row <= 8: | ||
+ | column = 1 | ||
+ | while column <= 8: | ||
+ | # Calculate E field for (row, column) | ||
+ | column += 1 | ||
+ | row += 1 | ||
+ | </ | ||
+ | In the above code, there are two separate while loops, with different conditional statements. The outer while loop (looping over rows) will only execute 8 times. Each time the outer loop executes, the inner loop (columns) will also execute 8 times. Together, the two loops will cover all 8 * 8 = 64 boxes in the chess board. | ||
+ | |||
+ | When you write code with a nested loop, it's important to carefully consider where each line of code belongs. In the above code, the column variable will be set to 1 eight times, moving back to the first column in each new row. The row variable is also only increased eight times, because it is not within the inner loop. If we forget to set the column variable back to 1, or increase the row variable 64 times, the code will not do what we intend. |