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
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
#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
j8k3mi8ll made it!
We have a be nice policy.
Please be positive and constructive.