While Loop Statements
Python utilizes the while
loop similarly to other popular languages. The while
loop evaluates a condition then executes a block of code if the condition is true. The block of code executes repeatedly until the condition becomes false.
The basic syntax is:
counter = 0
while counter < 10:
# Execute the block of code here as
# long as counter is less than 10
An example is shown below:
days = 0
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
while days < 7:
print("Today is " + week[days])
days += 1
Output:
Today is Monday
Today is Tuesday
Today is Wednesday
Today is Thursday
Today is Friday
Today is Saturday
Today is Sunday
Line-by-Line Explanation of the above CODE:
- the variable ‘days’ is set to a value 0.
- a variable week is assigned to a list containing all the days of the week.
- while loop starts
- the block of code will be executed until the condition returns ‘true’.
- the condition is ‘days<7’ which rougly says run the while loop till the point the variable days is less than 7
- So when the days=7 the while loop stops executing.
- the days variable gets updated on every iteration.
- When the while loop runs for the first time the line ‘Today is Monday’ is printed onto the console and the variable days becomes equal to 1.
- Since the variable days is equal to 1 which is less than 7 so the while loop is executed again.
- It goes on again and again and when the console prints ‘Today is Sunday’ the variable days is now equal to 7 and the while loop stops executing.