Form 4 - Computer
2024-2025
Floor 4 - Computer Room
Mr. Peter, Mr. Jon
While Loops

Santa Rosa de Lima English Secondary School
聖羅撒英文中學
Colégio de Santa Rosa de Lima - Secção Inglesa
Outline
What is While Loop in Python
1.
Review the concept of While Loop in Python
How Looping works in Python
2.
Focusing on the syntax of While Statement
Python While Loop Statement implementation
3.
Define while loop statement for requirements
What is While Loops?
What is While Loops in Python?
A piece of code in Python
What is While Loops in Python?
Code Start
What is While Loops in Python?
Code Start
Code End
What is While Loops in Python?
Code Start
Code End
Condition
What is While Loops in Python?
Code Start
Code End
Condition
IF True
What is While Loops in Python?
Code Start
Code End
Condition
IF True
Otherwise
While Loops Statement in Python
1.
2.
Tab
while Condition :
Command1

While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
Start Number
Define the condition for reaching the end
Specify the increment for each loop
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
Start Number
Define the condition for reaching the end
Specify the increment for each loop
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
1
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
1
2
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
1
2
True
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
1
2
True
2
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
1
2
True
2
2
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
1
2
True
2
3
False
While Loops Statement in Python
1.
2.
while i < 3 :
3.
Tab
4.
Tab
i = 0
print(i)
i = i + 1
i
_______
_______
_______
_______
_______
_______
i < 3
_______
_______
_______
_______
_______
_______
0
Output
_______
_______
_______
_______
_______
_______
True
0
1
True
1
2
True
2
3
False
End
End
End
End
End
End
End
While Loop Statement in Python
i = 2
while i <= 5:
print( i )
i = i + 1
Please determine the output of the following While statement:
2, 3, 4, 5
While Loop Statement in Python
i = 1
while i < 3:
print( i )
i = i + 2
Please determine the output of the following While statement:
1
While Loop Statement in Python
i = 4
while i != 0:
print( i )
i = i - 2
Please determine the output of the following While statement:
4, 2
While Loop Statement in Python
i = 1
while i < 1 or i > 1:
print( i )
i = i - 2
Please determine the output of the following While statement:
Nothing
Python While Loop Statement Exercises
Python While Loop Exercises - Ex01
Ex01 Demo
Please create a python question that ask student to write a python program which allow user to input a increment for each while loop from 1 to 100
1.
Save your file as "ClassNumber_YourName_Ex01.py"
increment = int( input( 'Enter a positive increment value: ' ) )
Prompt the user to input a positive integer to use as the increment.
1
2
Ensure the input is a positive integer. If not, display an error message and prompt again.
3
Use a while loop to iterate from 1 to 100, incrementing by the user's input.
4
Print each number generated by the loop.
Python While Loop Exercises - Ex01
Ex01 Demo
Please create a python question that ask student to write a python program which allow user to input a increment for each while loop from 1 to 100
1.
Save your file as "ClassNumber_YourName_Ex01.py"
increment = int( input( 'Enter a positive increment value: ' ) )
Prompt the user to input a positive integer to use as the increment.
1
2
Ensure the input is a positive integer. If not, display an error message and prompt again.
3
Use a while loop to iterate from 1 to 100, incrementing by the user's input.
4
Print each number generated by the loop.
Python While Loop Exercises - Ex01
Please create a python question that ask student to write a python program which allow user to input a increment for each while loop from 1 to 100
1.
Save your file as "ClassNumber_YourName_Ex01.py"
increment = int( input( 'Enter a positive increment value: ' ) )
Prompt the user to input a positive integer to use as the increment.
1
2
Ensure the input is a positive integer. If not, display an error message and prompt again.
3
Use a while loop to iterate from 1 to 100, incrementing by the user's input.
4
Print each number generated by the loop.
Python While Loop Exercises - Ex02
Ex02 Demo
Write a Python program that allows the user to input a number to be used as an increment or decrement in two while loops. The first loop should start at 1 and continue until it reaches or exceeds 100. The second loop should start at 100 and continue until it reaches or goes below 1. The program should print each value in both loops.
1.
Save your file as "ClassNumber_YourName_Ex01.py"
steps = int( input( 'Enter a positive increment/decrement value: ' ) )
Prompt the user to input a positive integer for incrementing from 1 to 100.
1
2
Use the same value for decrementing from 100 to 1.
3
Ensure the input is a positive integer. If not, display an error message and prompt again.
4
Use while loops to iterate and print values.
Python While Loop Exercises - Ex02
Ex02 Demo
Write a Python program that allows the user to input a number to be used as an increment or decrement in two while loops. The first loop should start at 1 and continue until it reaches or exceeds 100. The second loop should start at 100 and continue until it reaches or goes below 1. The program should print each value in both loops.
1.
Save your file as "ClassNumber_YourName_Ex02.py"
steps = int( input( 'Enter a positive increment/decrement value: ' ) )
Prompt the user to input a positive integer for incrementing from 1 to 100.
1
2
Use the same value for decrementing from 100 to 1.
3
Ensure the input is a positive integer. If not, display an error message and prompt again.
4
Use while loops to iterate and print values.
Python While Loop Exercises - Ex02
Write a Python program that allows the user to input a number to be used as an increment or decrement in two while loops. The first loop should start at 1 and continue until it reaches or exceeds 100. The second loop should start at 100 and continue until it reaches or goes below 1. The program should print each value in both loops.
1.
Save your file as "ClassNumber_YourName_Ex02.py"
steps = int( input( 'Enter a positive increment/decrement value: ' ) )
Prompt the user to input a positive integer for incrementing from 1 to 100.
1
2
Use the same value for decrementing from 100 to 1.
3
Ensure the input is a positive integer. If not, display an error message and prompt again.
4
Use while loops to iterate and print values.
F4 - Lesson04
By Mr. Jon Chong
F4 - Lesson04
- 391