Saturday, July 2, 2022

Python - While loops

# While loop
# Displays 1 through 5
i = 1
while i < 6:
  print(i)
  i += 1

print("---")

# break statement
# Displays 1 through 3
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
 
print("---")

# continue statement
# Displays 1 through 6
i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)

print("---------")

# Displays 1 through 5, and then executes the else statement
# While ... Else
i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i is no longer less than 6")