Slides badge

Conditional loops

Explain what is wrong with each program

for x = 0 to 5:
  print(x)
for x = 5 to 3:
  print(x)
  • What a loop is in computer science
  • How to implement a conditional loop in Python
  • Be able to explain benefit of loops in computer programming
  • Be able to develop a conditional loop in Python

Conditional loops

  • There are two kinds of loops: fixed loops and conditional loops
  • We've already looked at fixed loops and we've also already looked at conditions.

A conditional loop

  • A condition will always evaluate to True or False.
  • Conditional loops use a condition to loop whilst a condition evaluates to true.
  • Some conditional loops never evaluate to True and therefore are considered to be infinite loops.
  • Python has several types of conditional loops, but the one we are interested in is the while loop.

For each of the following, state whether it is true or false:

 

 

 

 

 

 

 

 

 

 

Task

x = True
y = False

print(x and y)
x = 54
y = 24

print(x > y)
x = 54
y = 24

print(x < y or y > 5)
x = True
y = True

print(not(x or y))

A while loop

  • While loops will continue to loop while a condition is True.
  • If the condition is initially false, then the loop will never run.
while True:
  print("This loop is never gonna end!")
n = 0
while n < 100:
  print(n)
  n = n + 1

While loops

counter = 0
while counter != 10:
  print ("Hello")

counter = counter + 1

A login system

print("Please insert your username")
username = input()
print("Please insert your password")
password = input()

if username == "Hamish" and password == "pass":
  print("Welcome!")
else:
  print("Login incorrect")

A login system - using loops

print("Please insert your username")
username = input()
print("Please insert your password")
password = input()
attempts = 0

while username != "Hamish" and password != "pass":
  print("Login incorrect")
  print("Please insert your username")
  username = input()
  print("Please insert your password")
  password = input()
  attempts = attempts + 1

print("Welcome", "It took you", attempts, "to login")

Conditional Loops

Functional requirements

  • Pseducode for a conditional loop is shown below. Write the Python code based on this pseudocode:

 

SET number TO 0
SET total TO 0
WHILE total <= 100:
ASK user to enter number
Total = Total + number
DISPLAY Total
END WHILE
DISPLAY Total

Complete the Conditional Loops worksheet

Task

Input validation

  • Input validation is a means of checking what a user has typed in is valid.
  • For example, if a program requires a number between 1 and 100 and the user typed 830, the data entered would be invalid.

Input validation in Python

  • The steps to validate input are:
    • Get the input
    • Loop while the input is not valid
      • Display an error message saying that the input is invalid
      • Ask the user to re-enter the value
print("Please insert a quantity")
quantity = int(input())

while quantity < 1 or quantity > 10:
  print("You must order at least one and fewer than ten items!")

print("You ordered " + str(quantity))
Presentation Overview
Close
JB
Conditional loops
© 2020 - 2026 J Balfour
23:58 | 18-01-2026
Join Live Session
Go Live
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Bullet Only Mode
Generate Quiz
Generate Lesson Plan
Widget Screen
Canvas Controls
Fullscreen
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Set a timer for how long?
    10:00
    5:00
    3:00
    2:30
    2:00
    1:00
    ...
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      Apply theme

      Blue theme
      White theme

      More effects:

      ×
      Loading
      All slideshow files