Academic Resource Center

While Loops in Python

Updated on

Python has two basics loops, While Loop and For Loop. We use a while loop when we repeatedly want to test a statement for as long as the conditional expression is true. If the conditional expression is false, the while loop ends. If the conditional expression is faulty from the beginning, the statement of the while loop will never get executed.

Python While Loop.pdf - Google Drive - Google Chrome

Syntax

Python While Loop.pdf - Google Drive - Google Chrome

Example

Consider an example where we ask a user to guess our favorite color. The conditional expression of our while loop that we want to test is

entry != myFavoriteColor.

The body of the loop is our statement that is executed if the conditional expression is true.

Python While Loop.pdf - Google Drive - Google Chrome
Python While Loop.pdf - Google Drive - Google Chrome

As you can see from the output above, every time a user inputs color that is not myFavoriteColor, the while loop executes the statement, asking the user What is my favorite color? When the user’s guess is finally blue, which matches myFavoriteColor, the conditional expression becomes “false,” and the while loop ends.

A very important point: We must ensure that the statement code can change the state of the conditional expression.

Additional While Loop Control Statements

Have you noticed from the previous example that we can execute while loop infinitely? A good habit is to use control statements, break or continue, exit, or skip some parts of the while loop.

Python Break Statement

Break statement is placed inside the while loop to break out the loop prematurely. After the break statement, no codes are executed inside the loop.

Python While Loop.pdf - Google Drive - Google Chrome

Syntax

Python While Loop.pdf - Google Drive - Google Chrome

Example

In our example, we can use the break statement to exit the while loop if the number of tries to guess the color is greater than the number of maximum attempts.

Python While Loop.pdf - Google Drive - Google Chrome
Python While Loop.pdf - Google Drive - Google Chrome

Python Continue Statement

Continue statement is placed inside the while loop to shortcut the loop skipping over an iteration to proceed to the testing of the conditional expression again.

Python While Loop.pdf - Google Drive - Google Chrome

Syntax

Python While Loop.pdf - Google Drive - Google Chrome

Example

In our example, we can use the continue statement to skip over one iteration of the while loop when the variable count is equal to two.

Python While Loop.pdf - Google Drive - Google Chrome
Python While Loop.pdf - Google Drive - Google Chrome

Ready to look at another concept in Python? Check out the If/Else Sheet guide here.

Need More Help?

Click here to schedule a 1:1 with a tutor, coach, and or sign up for a workshop. *If this link does not bring you directly to our platform, please use our direct link to "Academic Support" from any Brightspace course at the top of the navigation bar. 

Previous Article For Loops in Python
Next Article If/Else Statements in Python
Have a suggestion or a request? Share it with us!