In this lesson, you will understand to use the pass, break and continue statements to alter the flow of a loop. Also, it is used as a placeholder for future implementation of functions, loops, etc.
Python’s break and continue statements can change how a regular loop behaves. Loops repeatedly run through a block of code until the test expression returns true, but there are occasions when we want to stop the current iteration or even the entire Loop without running the test. In these situations, the break and continue statements are employed.
A break statement ends the loop that it is contained in. The innermost loop will end if the break command is used inside a nested loop (a loop inside another loop). The break command will end the deepest loop if contained within a nested loop (a loop inside another loop).
Basic Syntax
break
Let’s use an illustration to show how to use the break statement. Write a while loop, for instance, to display each character in a string, then exit the loop if a character is a !.
Example
# Use of break statement inside the loop for strval in "My name is saira! Forget": if strval == "!": break print(strval) print("Got it")
Output
M y n a m e i s s a i r a Got it
We loop through the given string sequence in this program. To exit the loop, we must determine whether the letter is ‘!’. As a result, we can see in the above output that every letter up to and excluding! is printed. The loop then comes to an end.
A loop’s continue command skips a code section just for this iteration. It does not end the loop but continues in the following iteration while ignoring the remainder of the loop’s body.
Basic Syntax
continue
Let’s look at an example of the continue statement in action. We loop through the string sequence in this program, and we must determine whether the letter is !. As a result, the above output shows that every letter up to and excluding ! is printed. The Loop then comes to an end.
Example
# Use of continue statement inside the loop for strval in "My name is saira! Forget": if strval == "!": continue print(strval) print("Got it")
Output
M y n a m e i s s a i r a F o r g e t Got it
The only difference between this program and the previous example is that the break statement has been changed to continue.
If the string character is ‘!’ we continue the loop rather than running the remaining code in the block. As a result, we can see in the program’s output that all letters other than ‘!’ is written.
In Python, the null statement is the pass statement. It is typically applied to empty functions or classes, and the execution of the pass statement has no effect.
Basic Syntax
pass
The interpreter returns no operation when it discovers a pass statement in the program. Think in Python, a remark and a pass statement vary in that comment is completely disregarded by the interpreter, whereas a pass statement is not. However, nothing happens when the pass is completed and has no operational impact (NOP).
Let’s use an illustration to show how to use the pass statement. The same thing is also possible with an empty function or class.
Example
# Use of pass statement inside the loop for strval in "My name is saira! Forget": if strval == "!": pass
Output
>