![]() |
VOOZH | about |
Python supports two types of loops: for loops and while loops. Alongside these loops, Python provides control statements like continue, break, and pass to manage the flow of the loops efficiently. This article will explore these concepts in detail.
A for loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range).
1 2 3
A while loop in Python repeatedly executes a block of code as long as a given condition is True.
0 1 2 3 4
Control statements modify the loop's execution flow. Python provides three primary control statements: continue, break, and pass.
The break statement is used to exit the loop prematurely when a certain condition is met.
0 1 2 3 4
Explanation:
The continue statement skips the current iteration and proceeds to the next iteration of the loop.
1 3 5 7 9
Explanation:
The pass statement is a null operation; it does nothing when executed. It's useful as a placeholder for code that you plan to write in the future.
0 1 2 3 4
Explanation:
Exercise: How to print a list in reverse order (from last to the first item) using while and for-in loops.