Explore
ProGrad Programs
Free Resources
Comprehensive Courses
Company-Specific Courses
Webinars
About Us

Edit
Reply




Edit

Control Statements in Python (Break, Continue & Pass) | FACE Prep

Published on 09 Mar 2020

As you may know, loops in Python are used to iterate repeatedly over a block of code. But at times, you might want to shift the control once a particular condition is satisfied. This is where control statements in Python come into the picture.


Control statements in python are used to control the flow of execution of the program based on the specified conditions.Python supports 3 types of control statements such as,


1) Break

2) Continue

3) Pass


Break in Python


The break statement in Python is used to terminate a loop. This means whenever the interpreter encounters thebreakkeyword, it simply exits out of the loop. Once it breaks out of the loop, the control shifts to the immediate next statement.


Also, if the break statement is used inside a nested loop, it terminates the innermost loop and the control shifts to the next statement in the outer loop.


Flowchart of Break Statement in Python


break control statements in python


Example of Break Statement in Python


Here is an example of how break in Python can be used to exit out of the loop. Let's say you want to check if a given word contains the letter A, then how would you do it?


#program to check if letter 'A' is present in the input
a = input ("Enter a word")
for i in a:
  if (i == 'A'):
    print ("A is found")
    breakelse:
    print ("A not found")


Input: FACE Prep
Output:
A not found
A is found


How is break statement useful in the above example? Our code accesses each character of the string and checks if the character is 'A'. Whenever it finds character 'A', it breaks out of the loop, hence avoiding the process of checking the remaining letters. So here break helps avoid unwanted looping.


Continue in Python


Whenever the interpreter encounters a continue statement in Python, it will skip the execution of the rest of the statements in that loop and proceed with the next iteration. This means it returns the control to the beginning of the loop.Unlike the break statement, continue statement does not terminate or exit out of the loop.Rather, it continues with the next iteration. Here is the flow of execution when continue statement is used.


Flowchart of Continue Statement in Python


continue control statement in python


Example of Continue Statement in Python


Let us see how the same above discussed example can be written using a continue statement. Here is the code.


#program to check if letter 'A' is present in the input
a = input ("Enter a word")
for i in a:
  if (i != 'A'):
    continueelse:
    print ("A is found")


Input: FACE Prep
Output:
A is found


Pass in Python


Assume we have a loop that is not implemented yet, but needs to implemented in the future. In this case, if you leave the loop empty, the interpreter will throw an error. To avoid this, you can use the pass statement to construct a block that does nothing i.e contains no statements. For example,


for i in 'FACE':
  if (i == 'A'):
    pass
print (i)


Output:
F
A
C
E


Control Statements in Python FAQs


1) How many control statements are present in Python?

In Python, there are 3 types of control statements. Namely, break, continue and pass statements.


2) How to break from a loop in Python?

The keyword break is used to break or terminate from the current loop. The control shifts to the next statement outside the loop.


3) What is the difference between break and continue statements in Python?

Break statement exits out of the loop, but the continue statement shifts the control to the next iteration and doesn’t exit out of the loop.


4) Can we use continue in if statement in Python?

Yes. Continue statements can be used within an if statement.


5) What is the use of pass keyword in Python?

Pass keyword helps in avoiding errors that result due to an empty loop.

If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'python'
Articles