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

419

5

2

Posted

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

Share

  • This is a dummy appl...-j8k3mi8ll

    j8k3mi8ll made it!

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • First Time Author Contest 2018

    First Time Author Contest 2018
  • Paper Contest 2018

    Paper Contest 2018
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

Comments