---
title: "Explain the Python While Loops with example."  
description: "Explain the Python While Loops with example."  
author: "Ravi Vishwakarma"  
published: 2025-09-24  
updated: 2025-09-24  
canonical: https://www.mindstick.com/forum/161927/explain-the-python-while-loops-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# Explain the Python While Loops with example.

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) While [Loops](https://www.mindstick.com/forum/23162/program-for-splitting-roommate-bills-problems-with-loops-and-broken-code) with example.**

## Replies

### Reply by Anubhav Sharma

> A `while` **loop** keeps running as long as a condition is **True**.\
> It’s useful when you **don’t know in advance how many times** you need to loop.

## Syntax

```python
while condition:
    # code block
```

- **condition** → a Boolean expression (`True` or `False`)
- The loop continues until the condition becomes **False**

## Examples

### 1. Basic while loop

```python
i = 1
while i <= 5:
    print(i)
    i += 1   # increase i
```

## Output:

```plaintext
1
2
3
4
5
```

### 2. Infinite loop (careful!)

```python
while True:
    print("This will run forever!")
    break  # use break to stop it
```

### 3. Using `break`

```python
i = 0
while i < 10:
    if i == 5:
        break
    print(i)
    i += 1
```

## Output:

```plaintext
0
1
2
3
4
```

### 4. Using `continue`

```python
i = 0
while i < 6:
    i += 1
    if i == 3:
        continue
    print(i)
```

## Output:

```plaintext
1
2
4
5
6
```

### 5. While loop with `else`

```python
i = 1
while i <= 3:
    print(i)
    i += 1
else:
    print("Loop finished!")
```

## Output:

```plaintext
1
2
3
Loop finished!
```

Key difference between `for` and `while`:

- `for` → best when you know how many times you want to repeat.
- `while` → best when you repeat until a condition changes.


---

Original Source: https://www.mindstick.com/forum/161927/explain-the-python-while-loops-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
