Menu Close

Keywords break, continue, and pass

In python, there are exception cases where there empty conditionals are needed or breaking out of a while loop forcefully. The following article will explain the usages for the break, continue, and pass keywords.

Break

The break keyword is used to break out of while loops. This allows for while loops to operate without the need of a conditional.

Example 1:

The code segment below simply shows the multiples of 25 up to 100

x = 0
while True:
    x += 25
    print(x)
    if x >= 100:
        break

Output:

25
50
75
100

Example 2:

The code segment below constructs a list of names given by the user’s input. The input “exit” represents the end of the string.

newList = []

while True:
    tempName = input()
    if tempName.lower() == “exit”:
        break
    newList.append(tempName)

print(newList)

Input:

John
Abby
Britney
Donald
Mike
exit

Output:

['John', 'Abby', 'Britney', 'Donald', 'Mike']

Continue

The continue keyword is used to end an iteration in a for loop or while loop. This skips all code that proceeds it and starts the next iteration.

Example 1:

This code will print every multiple of 10 below 100 that is also not a multiple of 3.

x = 0
while x < 100:
    x += 10
    if x % 3 == 0:
        continue
    print(x)

Output:

10
20
40
50
70
80
100

Example 2:

This code will remove all vowels from a string by converting it to a list of characters, then removing each vowel from the list.

vowelList = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
testString = “The quick brown fox jumps over the lazy dog”

testList = list(testString)
finalList = []

for i in range(len(testString)):
    if testList in vowelList:
        continue
    finalList.append(testList)

print(‘’.join(finalList))

Output:

Th qck brwn fx jmps vr th lzy dg

Pass

The pass keyword is used as filler for future code. It is used as a null statement as to not create errors from empty functions and conditionals.

It can also be used for an empty conditional if the code block requires the use of many elif statements.

Example 1:

The following example is of a function that does nothing.

def newFunction():
    pass

print(“No errors created”)

Output:

No errors created

Example 2:

The following code sorts values of a list that are not multiples of 5 into lists of multiples of 2, 3, and both.

testList = [63, 50, 15, 18, 22, 27]
twomultiples = []
threemultiples = []
both = []

for value in testList:
    if value % 5 == 0:
        pass
    elif value % 2 == 0 and value % 3 == 0:
        both.append(value)
    else:
        if value % 2 == 0:
            2multiples.append(value)
        if value % 3 == 0:
            3multiples.append(value)

print(‘Multiples of 2:’, str(twomultiples))
print(‘Multiples of 3:’, str(threemultiples))
print(‘Multiples of both:’, str(both))

Output:

Multiples of 2: [22]
Multiples of 3: [63, 27]
Multiples of both: [18]
Posted in Embedded Programming Language, Python

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!