Introduction: Teach Me Python #7: Control Statements Pt.2: While Loops

Welcome to the second part of the control loops tutorial. I am sorry about the recent lack of uploads but I have been fairly busy but here it is at last I hope you enjoy. Today we will be going over while statements and as always please rate and subscribe

Step 1: The Code

How while loops work is that they repeat something while a condition evaluates to true
Type the following code into python

i = 0
while i < 11:
i = i + 1
print(i)

If you run this code you should notice that it outputs the numbers 1 to 10 then the program ends. This is because it is being told that i equals 0 then on the next line it says 'execute the block of code below while i is below 10' on the next line it is increasing i by 1 then finally it prints it's new value

What if you wanted to loop a certain part of code for ever well you can do this by typing

while True:
#your code here

If you do this but want it to end if something happens you can use a break statement which is simply used by typing break where you want the code to stop looping

Comments

About This Instructable

393views

5favorites

License:

Bio: 1 new python tutorial every day Comment what you want the next one to be Please favourite and subscribe Notice Board: - since instructables has automatically ... More »
More by BlueBlood Studios:Teach Me Python #7: Control Statements Pt.2: While LoopsTeach My Python #6: Control Statements Pt.1: If, Else And ElifTeach Me Python #5: Simple Maths
Add instructable to: