• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
EDUCBA

EDUCBA

MENUMENU
  • Resources
        • Java Tutorials

          • Cheat Sheet Java
          • Cheat Sheet Python
          • C# vs Js
        • Java Tutorials
        • Python Tutorials

          • Angular 5 vs Angular 4
          • Careers in Python
          • Kali Linux vs Ubuntu
        • Python Tutorials
        • Top Differences

          • Cheat Sheet JavaScript
          • Python Interview Questions
          • Cloud Computing or Virtualization
        • Top Differences
        • Others

          • Resources (A-Z)
          • Top Interview Question
          • Programming Languages
          • Web Development Tools
          • HTML CSS Tutorial
          • Technology Basics
          • Technology Careers
          • View All
  • Free Courses
  • All Courses
        • Certification Courses

          Software Development Course 2
        • All in One Bundle

          All-in-One-Software-Development-Bundle
        • Become a Python Developer

          Python-Certification-Training
        • Others

          • Java Course
          • Become a Selenium Automation Tester
          • Become an IoT Developer
          • Ruby on Rails Course
          • Angular JS Certification Training
          • View All
  • 600+ Courses All in One Bundle
  • Login

While Loop in Python

Home » Software Development » Blog » Python Tutorials » While Loop in Python

While Loop in Python

Overview of While Loop in Python

In this article, we will discuss specifically While loop in Python. Python is a language that is in great demand in today’s market mainly because it is open-source, easy to write and understands and has a variety of open-source libraries which makes it easier to build and deploy models. Now coming to a while loop. It is very commonly used in programs. It is an entry controlled loop and we use it if we want to execute any group of instructions repeatedly based on a Boolean condition.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

while expression:
body of the loop

Flow Chart

The flow of execution for while loop is shown below. This flow chart gives us the information about how the instructions are executed in a while loop.

Flow Chart for while loop in python

How While Loop works in Python?

After going through the syntax and flow we will now understand how the flow actually works. Before we enter the while loop, there is a condition check basically it is an expression that returns the Boolean result which means the output of the expression will either be true or false. If and only the expression returns true that the control is allowed to enter inside the loop and execute the instructions present inside the loop. One the instructions in the body of the loop are executed for the first time the control again goes to the top of the loop where the entry expression or condition is present if the expression returns true the control executes the same instructions present in the body of the loop again and if the expression returns false the control comes out of the loop. The while loop is also called entry controlled loop as the entry of the control inside the loop firmly depends on whether the expression returns true or false.

Since we have discussed how the control flows inside the while loop, let’s see some examples.

Example #1

The first example is a simple one. Here the problem statement is to add the first 10 natural numbers.
This means we will add numbers starting from 1 and end at 10. Since we will use a while loop so we will provide a condition and also have to keep a variable as counter as we have to increase the counter by one after each addition.

Code:

n=10  # upper limit
# initializes the variable for sum
sum =0
i=1
while  i<=n:
sum= sum+ i
i=i+1     #increment the counter
# print the sum
print("the sum :",  sum)

Popular Course in this category
Cyber Week Sale
Python Certification Training (36 Courses, 12+ Projects) 36 Online Courses | 12 Hands-on Projects | 187+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.8 (3,488 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects)Angular JS Certification Training (9 Courses, 5+ Projects)

So, now as you can see first of all the expression checks whether the condition is true or false. In our case, it is true the first time as 1 is less than 10. So it goes inside and adds zero with one and stores the value in the sum variable. It then updates the counter by one. This process continues until the counter increases to 11, as at that point the expression returns false and the control comes out of the loop and prints the sum.

Output:

While Lop in Python-1.1

Example #2

The next example is on infinite while loop which means that it will go on executing the loop infinitely. We should always try to avoid this situation as this situation will not allow the program to terminate. It generally happens due to the expression statement in this case always returns true

Code:

n=1
print("Infinite loop starts")
while n>0:
n=n+1
print(n)

If we take a close look at the above example we will see that the conditional expression n>0 is true first time as an initial value of n is 1. The control then enters the loop and increases n by 1 and then it executes the two print statements. Upon execution, it again goes to the top of the loop and checks the expression which is again true as the value of n is now 2. This flow repeats itself again and again as the starting expression is always true and the value of n keeps incrementing by 1 each time. This leads us to the scenario of an infinite loop as it never terminates.

Let us now look into a different example in which we will also use conditional statements like if-else along with while loop. The below screenshot shows infinite loop.

Output:

While Lop in Python-1.2

Example #3

Let us print the even and odd numbers present between 1 and 10. Which means the lower limit is 1 and the upper limit is 10. The counter will have initial value as one since we will start at 1 and then keep on incrementing the counter by one.

Code:

n=10
i=1
while i<=n:
if  i%2==0:
print("Even Number" , i)
i= i+1
else:
print("Odd Number" , i)
i=i+1

So, as you can see in the above code we have the expression to check whether the number is less than 10 or not. In the first case, the expression will return true and hence the control will flow inside the body of the loop where it contains conditional statements if-else. During the first case, the value of i is one so the modulus will not be zero hence if a condition will fail and the else condition statement will get executed. After that, the value of the counter will be incremented by one. In the second case if a condition will be true and hence the statement under if a condition will be executed. The value of the counter will again be incremented by one. This will be continued until the value of i becomes 11. In that case the while expression fails and returns false. Immediately the control goes out of the loop.

Output:

incrementing the counter by one output

Recommended Articles

This is a guide to While Loop in Python. Here we discuss the overview of while loop in python and how while loop works in python along with its example. You may also look at the following articles to learn more-

  1. String Array in Python
  2. Patterns in Python
  3. Swapping in Python
  4. Python Nested Loops
  5. Introduction to While Loop in PHP
  6. Python Sets
  7. Python Features
  8. String Array in JavaScript

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar
Technology Blog Tutorials
  • Python Tutorials
    • Inheritance in Python
    • String Formatting in Python
    • Sorting in Python
    • Python List Comprehension
    • Arrays in Python
    • Socket Programming in Python
    • List Comprehensions Python
    • Python Regex
    • Matplotlib In Python
    • If Statement in Python
    • Dictionary in Python
    • Object in Python
    • Python Overloading
    • Reverse Number in Python
    • Fibonacci Series in Python
    • Python Keywords
    • Python Sets
    • Bubble Sort in Python
    • Heap Sort in Python
    • Python Data Types
    • Recursive Function in Python
    • Do While Loop in Python
    • Multidimensional Array in Python
    • Python Variable Types
    • Comments in Python
    • Python Features
    • Python Variables
    • Python Database Connection
    • While Loop in Python
    • Destructor in Python
    • Best Compiler for Python
    • Python IDE for Windows
    • Pandas.Dropna()
    • Math Functions in Python
    • Python Infinite Loop
    • Abstract Class in Python
    • String Array in Python
    • Python Editors
    • List Operations in Python
    • Python Nested Loops
    • Loops in Python
    • Swapping in Python
    • Python Bitwise Operator
    • Palindrome in Python
    • For Loop in Python
    • Factorial in Python
    • Encapsulation in Python
    • Python Exception Handling
    • Python File Operations
    • Random Number Generator in Python
    • Star Patterns in Python
    • Python Libraries For Data Science
    • If Else Statement in Python
    • Boolean Operators in Python
    • Constructor in Python
    • Python Comparison Operators
    • 2D Arrays In Python
    • Patterns in Python
    • Pointers in Python
    • 3d Arrays in Python
    • Python Collections
    • Advantages of Python
    • Is Python Object Oriented
    • How To Install Python
    • What Is Python
    • Is Python Open Source
    • Python Operators
    • Limitations of Using Python
    • Python Socket Programming
    • Violent Python Book Review
    • Python Programming
    • New Future of Python
    • Python Programming for Non Engineering
    • Python Programming
    • Gray Hat Python: Security
    • Python Fast And python psyco
    • Python Squeezes the Web
    • Python Programming
    • Python and Django for Web Development
    • Bash Scripting Programming and Python
    • Careers in Python
    • Uses of Python
    • Cheat Sheet Python
    • Uses Of Django
    • Sequences in Python
    • Python Programming for Absolute
    • Is Python a scripting language
    • Introduction To Python
    • Python Alternatives
  • Database Management (71+)
  • Ethical Hacking Tutorial (33+)
  • HTML CSS Tutorial (47+)
  • Installation of Software (54+)
  • Top Interview question (188+)
  • Java Tutorials (196+)
  • JavaScript (71+)
  • Linux tutorial (32+)
  • Network Security (85+)
  • Programming Languages (232+)
  • Software Development Basics (321+)
  • Software Development Careers (38+)
  • SQL Tutorial (33+)
  • String Functions (12+)
  • Technology Commands (38+)
  • Top Differences (368+)
  • Web Development Tools (33+)
  • Mobile App (60+)
Technology Blog Courses
  • Python Certification Course
  • Programming Languages Courses
  • Angular JS Certification Training
Footer
About Us
  • Who is EDUCBA?
  • Sign Up
  •  
Free Courses
  • Free Course Programming
  • Free course Python
  • Free Course Java
  • Free Course Javascript
  • Free Course on SQL
  • Free Course on Web Design
  • Free HTML Course
  • Free Android App Development Course
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • Ruby on Rails Course
  • ASP.NET Course
  • VB.NET Course
  • Bootstrap Training Course
  • Become a Linux System Administrator
  • PHP Course
  • Joomla Training
  • HTML Course
Resources
  • Resources (A To Z)
  • Java Tutorials
  • Python Tutorials
  • Top Differences
  • Top Interview Question
  • Programming Languages
  • Web Development Tools
  • HTML CSS Tutorial
  • Technology Basics
  • Technology Careers
  • Ethical Hacking Tutorial
  • SQL Tutorials
  • Digital Marketing
Apps
  • iPhone & iPad
  • Android
Support
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions

© 2019 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Let’s Get Started
Please provide your Email ID
Email ID is incorrect

Cyber Week Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More

Cyber Week Offer - Cyber Week Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More