Article 014
In today's article we will discuss about the functionality of 'continue' and 'break' statements inside a loop.
continue: This is used to skip a particular value inside a variable. A simple use-case of this is to extract information of all students inside a class except for those who are transferred. For transferred students, we could simply use 'continue'.
break: This is simply used to continue the loop until the desired set is extracted from the available dataset. A simple use-case of this is to get 1st 30 students from a class of 50 students.
Let's dive into a simple example for better understanding.
Suppose a class has 5 students Ram, Shyam, Geeta, Sita and Golu
where Sita and Geeta are newly transferred students.
Now we can get a list of existing students by simply using continue.
Student = ['Ram', 'Shyam', 'Geeta', 'Sita', 'Golu']
for i in Student:
if i == 'Sita' or i == 'Geeta':
continue
print(i)
Output is
Ram
Shyam
Golu
Similarly we can get students names up to Geeta by simply using break.
Student = ['Ram', 'Shyam', 'Geeta', 'Sita', 'Golu']
for i in Student:
if i =='Sita' :
break
print(i)
Output is
Ram
Shyam
Geeta
Hope the above example is helpful in better understanding of 'break' and 'continue' statements in Python