Control flow - conditional blocks and loops Flashcards
pass statement
The pass statement is used as a placeholder for future code.
When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed.
Empty code is not allowed in loops, function definitions, class definitions, or in if statements.
e.g. def myfunction():
pass
break
With the break statement we can stop the loop before it has looped through all the items:
fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print(x)
if x == “banana”:
break
Output: apple
continue
With the continue statement we can stop the current iteration of the loop, and continue with the next:
fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana”:
continue
print(x)
output: apple cherry