Control flow - conditional blocks and loops Flashcards

1
Q

pass statement

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

break

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

continue

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly