184_notes:loops

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
184_notes:loops [2022/04/20 14:35] – [While Loops] woodsna1184_notes:loops [2022/05/07 01:19] (current) woodsna1
Line 26: Line 26:
  
 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.  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 ====
  
-===== While Loops =====+{{youtube>SONwVUMB_Q0?large}}  
 + 
 +==== While Loops ====
  
 Here's another example of a while loop: Here's another example of a while loop:
Line 83: Line 86:
     row += 1     row += 1
 </code> </code>
-This code is different.+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.
  • 184_notes/loops.1650465347.txt.gz
  • Last modified: 2022/04/20 14:35
  • by woodsna1