top of page

6. Loops

What are Loops?

In programming, loops are a sequence of instructions that continually repeat until a certain condition is met.

Why Loops?

When you write code, loops allow you to shorten what could be hundreds of lines of code into just a few.

Types of Loops

There are two types of loops in Python: "for" loops and "while" loops.

"while" Loops

"while" loops repeat as long as a certain boolean condition is not met.

"for" Loops

"for" loops iterate over a given sequence. For loops are great to access individual items in a sequence, such as a list.

How do you Create "while" Loops?

First, type in the word "while".

while

Then, type in the conditional you are checking and a colon after that.

while x > 2:

Finally, add the code you want to execute while the condition remains true.

while x > 2:
 
print(x)
 
z = x

How do you Create "for" Loops?

First, type in the word "for".

for

Then, type in value name that will assign itself to the item of the sequence.

for x

Next, type the word "in" and the name of the sequence you want to iterate. End the statement with a colon.

for x in list_of_numbers:

Finally, type in the code you would like to execute using the value that assigned itself to the item.

for x in list_of_numbers:
     z = x
     
print
(x)

The "break" Statement

A cool thing about loops is you can create conditionals inside of them. This is useful if you want the code to execute something based on if a condition happens while the loop is running.

while x > 2:
   
if x == 2:
      
print(x)
      
z = x
   
else:
      print("Nothing")

For example, let's say you want to break out of a loop if a certain condition is met. How could you do that?

It is actually very simple. To end a loop, all you have to do is type the word break. Look at the example below to understand this better.

while x > 2:
   
if x == 2:
      
break   
   
else:
      print(
x)     
      x++

In this code block, once x is equal to 2, the code will break the loop. However, if x is not equal to 2, it will print out the value of x and add one to x.

And that's all there is to the break statement. As you progress, you will use this in more complex scenarios. But for now, feel free to create your own programs using the break statement.

The "continue" Statement

The continue statement returns the program back to the beginning of the loop. Let's use the example from above to explain this in more detail.

while x > 2:
   
if x == 2:
      
break   
   
else:
      print(
x)     
      x++

What if we wanted the program to repeat the loop once x reaches 2 instead of breaking out of the loop?

It's actually quite simple. all you need to do is replace the "break" with "continue"

while x > 2:
   
if x == 2:
      
continue   
   
else:
      print(
x)     
      x++

Now, when x equals 2, the program will go back to the beginning of the loop.

And now that's all that there is to loops. And you have also finished all of The Basics course. You know have the skills to build your own programs at a basic level. To build your skills even further, head over to the Advanced Topics course. Thank you!

bottom of page