While Loop Statements

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use while loop statements, break statements, and continue statements in Python. These control structures allow us to write code that can repeat a block of statements, or skip certain statements within a loop, or exit a loop early.

Achievements

  • while Loop Statements
  • break Statements
  • continue Statements

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/ControlFlowGroup -.-> python/break_continue("`Break and Continue`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-97{{"`While Loop Statements`"}} python/booleans -.-> lab-97{{"`While Loop Statements`"}} python/conditional_statements -.-> lab-97{{"`While Loop Statements`"}} python/while_loops -.-> lab-97{{"`While Loop Statements`"}} python/break_continue -.-> lab-97{{"`While Loop Statements`"}} python/build_in_functions -.-> lab-97{{"`While Loop Statements`"}} end

While Loop Statements

A while loop in Python allows us to execute a block of statements repeatedly as long as a certain condition is met. Here is the general syntax:

while condition:
    ## code block to be executed

The code block inside the while loop will be executed as long as the condition is True. If the condition becomes False, the while loop will exit.

Here's an example of a simple while loop that counts from 1 to 5:

Open up a new Python interpreter.

python3

Type the following code:

count = 1
while count <= 5:
    print(count)
    count += 1

Exercises

  1. Write a while loop that counts from 10 to 1 and prints each number.
  2. Write a while loop that counts from 1 to 10, but only prints even numbers. Hint: use the modulo operator (%) to check if a number is even.

Break Statements

Sometimes, we may want to exit a loop early based on certain conditions. We can use the break statement to do this. When the break statement is encountered within a loop, the loop is immediately terminated and control is transferred to the next line of code after the loop.

Here's an example of using the break statement to exit a loop early:

count = 1
while True:  ## this will create an infinite loop
    if count > 5:
        break  ## exit the loop when count becomes greater than 5
    print(count)
    count += 1

Exercises

  1. Write a while loop that counts from 1 to 10, but exits early when the count becomes greater than 5.
  2. Write a while loop that prompts the user to enter a number. If the number is even, print "Even" and exit the loop. If the number is odd, print "Odd" and continue with the next iteration.

Continue Statement

The continue statement allows us to skip the rest of the current iteration and move on to the next one. When the continue statement is encountered within a loop, control is immediately transferred to the beginning of the next iteration.

Here's an example of using the continue statement to skip the rest of the current iteration:

count = 1
while count <= 10:
    if count % 2 == 0:  ## if count is even, skip the rest of the current iteration
        count += 1
        continue
    print(count)  ## this line will only be executed if count is odd
    count += 1

Exercises

  1. Write a while loop that counts from 1 to 10, but only prints odd numbers. Hint: use the continue statement to skip the rest of the current iteration if the number is even.
  2. Write a while loop that prompts the user to enter a number. If the number is less than 0, print "Invalid input" and continue with the next iteration. If the number is greater than or equal to 0, print "Valid input" and exit the loop.

Summary

In this lab, we learned how to use while loops, break statements, and continue statements in Python. While loops allow us to repeat a block of code multiple times, while break statements allow us to exit a loop early, and continue statements allow us to skip the rest of the current iteration and move on to the next one. These control flow statements are useful tools for controlling the flow of execution in our programs.

Other Python Tutorials you may like