Loops
Say we want to find the first 10 nonegative numbers. We could create a function that just prints all those numbers, ie. 0, 1, 2, ..., 9, but that seems like a lot of work. And imagine if we wanted to print the first 100 numbers! Fortunately, programming allows us to use loops to simplify processes like this.
A for loop runs an action a specified number of times. An example is shown below:
Now, say we want to print the nonnegative numbers between x and y. We need to specify y as another argument of the for loop.
We see that now the first argument in the for loop, x, is where the loop begins; the second argument, y, is where the loop ends, but not including the y number. Note that this is equivalent to a range of [x, y), where y isn't included. So, the numbers that are printed are actually 3 through 9.
Now suppose we only want every other number, i.e. 3, 5, etc. We add yet another argument:
Now we see that this function prints 5 numbers: 3, 5, 7, and 9. The third argument that is passed into a for loop specifies how much we step by when looping through the range [x, y).
What if we want these numbers, but in descending order instead? Let's think about this step by step:
First, if we're going in descending order, then we start at number y. However, remember that y was not included in the previous loop--the loop ended printing number y-1. So, we should actually start the loop at y-1.
Second, we should end the loop at number x. Following the same logic, if we want to actually include x, we should include it as x-1.
Lastly, how much do we step by? Well, we want every zth number in descending order: hence, we should step by -z.
Let's do one more example: determining whether or not a given number n
is a prime number.
To do so, we want to check every number between 2 and n
to see if the number can fully divide n
.
Nested for loops
We can actually put loops inside of other loops. This is expecially useful when we deal with variables that may have more than one dimension. For instance, think about x and y values on a graph.
Let's try and create a 2D-shape with nested for loops:
Here's a challenge: try and draw a right triangle in the same format, with the hypotenuse going from the upper right corner down to the bottom left corner.
Some of you might be confused by this.
Note that row
, like i
that you've seen in other loops on this webpage,
are actually variables! That means you can use them in other parts of the function.
Hence, when row
increases with each iteration, more columns of stars are printed out!
While loops
For loops are great, but some problems that require loops might not necessarily have "ranges" to loop through. For instance, if we want to find the nth prime number, we don't have a specific range to look through-- we kind of just have to look at numbers one by one and determine if they're primes. For this, we can use the isPrime(n) function we wrote before.
A while loop will keep running as long as what's between its brackets evaluates to True. Hence, in the above function, the loop will keep checking whether or not numbers are primes, and if they are, record the number of primes we've found, all the way until we've found the nth prime.