Hey how are you doing? So, today we will be learning about while loops. Before I start just wanted to tell you that if you don’t know what loops are, their applications or what a for loop is, I recommend checking my last post. Also, if you don’t know what loops are you will be lost in this session, so please check this out :
Alright, now since everyone is ready, let’s get this started.
WHAT IS A WHILE LOOP?
While loop executes a code as long as an expression is True. For
example:
In this example, the code inside
the while loop executes as long as the value of ‘x’ is greater than 0. The code
prints the value of ‘x’ and decreases the value by 1 each time the loop executes. Eventually the value of ‘x’ will be 0
and that is where Python will stop executing the while loop and print the next
statement i.e., ‘Happy New Year’.
Note: While loop with an expression that always evaluates to True
will run forever. For example,
x=1
while x>0:
print(x)
x+=1
WARNING: DO NOT TRY THIS UNLESS YOU KNOW HOW TO STOP THE LOOP
OR ELSE YOU WON’T BE ABLE TO STOP THE LOOP AND YOUR CONSOLE MAY CRASH. YOU HAVE
BEEN WARNED !!!!
JUST KIDDING !!! If you are using Python
IDLE, simply press Ctrl + c and the program will stop its execution.
‘break’ KEYWORD:
The ‘break’ keyword is used to end a loop. For
example,
Here, we set the range from 1 to
100. So basically, the loop will run 99 times (because it does not include the
last number) and print the value of ‘i’ each time. So this is what the output
of this program should look like:
But in the actual program, this is
what the output looks like:
WHY??
Because we used the ‘break’
keyword. The ‘break’ keyword tells
Python to stop executing the loop. This is why, the execution stopped after
it came across the ‘break’ keyword in the very first iteration.
‘continue’ KEYWORD:
The ‘continue’ keyword stops the execution of the current iteration of
the loop and moves to the next iteration. For example,
In this example, when the value of
‘i’ became ‘3’ the ‘if’ statement inside the loop evaluated to True. Inside the
‘if’ statement, Python came across the ‘continue’ keyword which basically tells Python to skip this iteration and move on to the
next one. So Python stops executing that iteration thus not printing the
value of ‘i’ and moves on to continue executing the loop normally.
NESTED LOOPS:
A loop with another loop inside it is called a nested loop. For
example:
for i in range(10):
for j in
range(i+1):
print('*',end=' ')
print()
I will not show the output of this
program. I want you to run this code and see the output for yourself, trust me
you will love it. Try changing the output by changing the values. Like I always
say have fun with it, that way you will never lose the love for learning
programming.
CONGRATULATIONS !!! You have learnt about while loops in Python. Now you know how to use Python to execute the same code as may time as you want. Trust me this knowledge will help you create programs that are a lot easier to understand and execute. Have fun with it !!! Create some cool programs. Good luck !!!!
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T feel free to ask any further queries in the comment section below.
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback