Exercise 6: While loop

A while loop repeats code until the condition is met. Unlike for loops, the number of iterations in it may be unknown.  A while loop always consists of a condition and a block of code.

A while loop ends if and only if the condition is true, in contrast to a for loop that always has a finite countable number of steps.

while loop

 
Related course: Complete Python Bootcamp: Go from zero to hero in Python

Definition

The while loop below defines the condition (x < 10) and repeats the instructions until that condition is true. Type this code:

Executes the code below until the condition x < 10 is met.   Unlike a for loop, the iterator i is increased in the loop.

Study drill

Try the exercise below:

1. Make a program that lists the countries in the set below using a while loop.

Previous
Next

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">