Professional Documents
Culture Documents
Python Continue, Break and Pass
Python Continue, Break and Pass
**`continue` statement**:
- When Python encounters a `continue` statement in a loop (like `for` or `while`), it immediately
jumps to the next iteration of the loop.
- It's useful when you want to skip certain iterations of a loop based on a condition without executing
the rest of the loop's code.
2. **`break` statement**:
- When Python encounters a `break` statement in a loop, it immediately exits the loop, regardless of
the loop's conditions.
- It's handy when you want to prematurely exit a loop based on a condition without executing the
remaining iterations.
3. **`pass` statement**:
- The `pass` statement is a null operation, meaning it does nothing.
- It's used as a placeholder when a statement is syntactically required but you don't want any action
to be taken.
- It's often used as a placeholder for code that will be added later.
```python
for i in range(1, 6):
if i == 3:
continue # Skips the current iteration when i equals 3
print(i)
```
```python
for i in range(1, 6):
if i == 3:
break # Exits the loop when i equals 3
print(i)
```
In this example, the `break` statement stops the loop when `i` equals 3, so only 1 and 2 are printed.
```python
for i in range(1, 6):
if i == 3:
pass # Does nothing when i equals 3
print(i)
```
In this example, the `pass` statement has no effect on the loop's behavior, so all numbers from 1 to 5
are printed.