Have you ever been asked to write “I will never be late for class” 500 times? If yes, did you wish you could write just one line and make your pen write the remaining 499 using the first as template? Did you wish you had magic powers to make that happen? What you wanted was automation. A way to do something over and over till a condition is met. In this case the condition is till you get to line 500.
Usually, you will create a loop that runs a block of code until a condition is satisfied. If you remember, conditions are expressions that evaluate to true or false.
In the example above, where you want to write “I will never be late for class” 500 times, you may put the code
print 'I will never be late for class' in a block that will run it until the 500th print statement is outputted.
The most common types of loops known in all programming languages are the while loop and for loop.
We will talk about while loop Today and take a look at for loop in our subsequent lessons.
In this loop, you do ‘something’ as long as a certain condition remains satisfied. In other words, while condition is satisfied, do this.
Let’s take a look at the syntax.
while condition_is_true: #do something #do another thing
The while keyword is used, followed by condition, colon then the statements. Let’s take a look at a typical while loop code.
count = 0 while count < 500: print 'I will never be late for class again' #do this count = count + 1 #do this to ensure count goes up, when it finally hits 500, it'll stop
In the code above, our condition is
count < 500. This means that, as long as count is less than 500, the statements under the while loop will be run. Count is incremented each time, that’s one very important thing to note about while loops. Without the code
count = count + 1, the condition will remain true forever because count will remain 0. This will result in what is known as infinite loop. A never ending loop.
Beware of infinite loops! it is a common mistake most new programmers (and even veterans) make sometimes.
Get creative with the syntax. Try a lot of things, change the condition, change the statements, make mistakes and comment what you found 🙂
As you write more complex code, you would like to be able to jump over certain blocks of codes and also break out of loops based on some conditions. This may sound so advance right now but don’t worry we’ll take a look at an example shortly.
The break keyword, stops a loop at that point and passes control to the rest of the code. The continue keyword on the other hand, causes the loop to skip the rest of the code in the loop and go back to the start of the loop.
count = 0 while count < 10: print "count hasn't gotten to 10" if count == 5: #check if count is 5 print "count hasn't gotten to ten but 5 is ok..." break #break out of while loop count = count + 1 #increment by 1
Run the code above and see the output.
print "count hasn't gotten to 10" is run 6 times (0-5) then
print "count hasn't gotten to ten but 5 is ok..." runs then break runs and ends the loop.
The condition says
count < 10 and 5 is less than 10 but the
break causes the loop to break, breaking the rule of the loop.
count = 0 while count < 10: #run block as long as count is less than 10 count = count + 1 #increment by 1 if count == 5: #check if 5 print "skipping for 5..." continue #jump the code beneath this line and move to the top of loop print "count hasn't gotten to 10"
Always take your time and think of what the code does on each line. Running this code, you will realize that
print "count hasn't gotten to 10" runs only 4 times before
print "skipping for 5..." is run. This is because, count is incremented first before the rest of the code runs, unlike the previous code which has
count = count + 1 as the last line.
This means that before “count hasn’t gotten to 10” is outputted for the first time, count is already 1.
Also, note that
print "count hasn't gotten to 10" doesn’t run at all on the 5th line where
print "skipping for 5..." runs. This is because of the
continue keyword. It causes the code to jump to the top of the loop.
count = count + 1 at the bottom and see what happens. Comment your finding below.
Now that you’ve started with loops, you’re a real programmer 🙂 Be proud! In our next lesson, we’ll talk about lists so we can move to for loops. For loops move hand in hand with lists (or arrays as known in languages like C, C++ and Java). See you soon fresh programmer!
- Declare a variable count and set it to 20
- Create a while loop that runs as long as count is greater than 0
- print the “we have gotten to number __” where the underscore is the value of count.
- skip the print statement when count gets to 10 and print out “I’m taking a breather”
You can’t concatenate string and integers, either convert the integer to string or use the .format() function.
It isn’t everyday that you must use count = count + 1, you can reduce count also.