Featured Post

How To Use While Loops In Python

How To Use While Loops In Python

Overview 

This beginners Python guide will explain the steps for working with 'while' loops in Python for beginners.




Introduction 


The Python while loops help to execute a set of statements in Python functions,  as long as a particular specified condition is True. Let us understand the while loop with a simple python example exercise, of a while loop usage, where we are trying to count numbers from 1 to 10, with a condition of the number being less than 10. We create a variable called num, and initiate it with a initial value of 1 and we carry out iteration of this num variable, whereby, it is incrementing every time it loops, by incrementing by 1. 

Code

num = 1
while num < 10 :
      print(num)
      num += 1

# output  
1
2
3
4
5
6
7
 8
 9

In the above case, we performed iteration of the while loop where every time the loop iterates, the num variable gets incremented by 1 and since the condition is that the num has to be less than 10, the iteration stops at the point when num reaches 9 and exits the loop. The task of the while loop above was to print num as long as num is less than 10, which is why it stops printing num values when it reaches value of 9 as 9 is still less than 10 but the next value is not printed out as it will be 10 and 10 is not less than 10.
Next, we are going to consider loop flow controllers that will change the default behavior of the while loop (where the iteration continues and repeats as long as the while condition is True). These loop flow controllers are the break statement,  the continue statement and the if-else statements and we are going to understand how these loop controllers are going to change the default flow behaviour of the while loop, by considering the below examples.


While loop with Break Statement


Now let us see how the while loop works with a break statement. 

In the case of a while loop, with a break statement,  we could  stop the loop, even when the while condition is True.  Let us understand this through an example.

Code

num = 1
while num < 10 :
      print(num)
      if num == 4 :
          break
      num += 1

# output  
1
4

 

As noted in the above case it is observed that the loop or iteration in the loop stops when num equals the value of 4. In other words due to the break statement,  the program exits the loop when num reaches the value of 4.
Hence, in the outcome we get values of num from 1 till 4.


While Loop with Continue Statement 


Let us understand how the while loop works with the continue statement. When num reaches value of 4, it will stop the current iteration and continue with the next iteration which is why, 4 is missing in the result, as it jumps to the next iteration the moment num is 4 as observed in the below code output.


Code

num = 1
while num < 6 :
      num += 1
      if num == 4 :
          continue
      print(num)
      

# output     1
                  2
                  3
                  5
                  6

 


While Loop with if-else Statement 


In this case we will see what happens when while loop has an if-else statement inside it.

Code 


num = 1
while num < 6 :
      print(num)
      num += 1
 else :
      print("num is no longer less than 6")
      
      

# output    1
                  2
                  3
                  5
                  num is no longer less than 6

 

From above code you can see that when we use the while loop with if-else statement,  as long as the num is less than 6, which is the while loop condition,  the loop is executed and keeps printing the numbers every time by incrementing by one. But, once the num is no longer less than 6, it will stop the loop and print out the statement that num is no longer less than 6. So, when the program printscout the message, we know, that the condition is false now.


Conclusion 


We learnt what is a while loop and how to work with while loops in Python. We also learnt how the while loop works with break statement, continue statement and if-else statement respectively. 

These loop controllers were successful in changing the default loop flow of the while loop. 

The break statement controls the while loop by exiting the loop iteration of the while loop even if the while condition is True. 

The continue statement will stop executing the current code block and jump into the next iteration thereby ignoring or skipping a particular value.

The if-else statement controls the while loop by conditionally executing a block of code in the while loop. The if code  block is executed when the condition is true, that is, if the if statement is true and the else code block is executed when the condition is false, that is , if the if statement is not true.

Please note, in the code block above, the alignment is horizontal instead of vertical, but, that is due to the code editor space alignment , ideally, all the code runs by jumping from one line to the next line following the indentation, but, it is difficult to demonstrate it ideally in the code editor of blogger due to some limitations in this code editor used here. If you follow the same code in any of the code editors such as Replit or Notepad or VS Code or any other faorite code editor of your choice, you will get the indentation right automatically when you start to write the code.

Comments

Popular Posts

Build A Random Quote Machine in React

Welcome to my first Blog Post

A Simple Guide to Promises in JavaScript ES6

Build A Calculator in React

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Guide to Learn Coding Efficiently and Effectively