Break, Continue and Pass in Python
The break statement in Python uses only within the loops.
The break statement stops the current iteration of loop and exit.
for i in range(10): print(i) if(i == 5): break print("End of program")OUTPUT
0 1 2 3 4 5 End of program
In the above program, as we can see we iterate the loop using
range(10), which generates the sequence from 0 to 9, but
as the condition get true within
break statement gets executed and loop terminates immediately.
NOTE : Break, only terminate the current loop in which it occurs.
The continue statement stops the current iteration of loop and continue the next iteration.
for i in range(10): if(i == 5): continue print(i) print("End of program")OUTPUT
0 1 2 3 4 6 7 8 9 End of program
As we can see, when i's value is 5, it stop the current iteration and move to next iteration.
Pass Statement :
Pass statement is never executed.
Used when a statement is syntactically required and do not want any code to execute now.
If there is a need to implement code in future.
Behaves like a placeholder for future code.
Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. They cannot have an empty body. The interpreter will generate error. So, we use the pass statement to construct a body that does nothing.
for x in [0, 1, 2]: pass
Next topic is String in Python
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at email@example.com
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at firstname.lastname@example.org