# While Loops

{% embed url="<https://www.youtube.com/embed/OShOW_ayPvA>" %}

&#x20;[Control Flow](http://python.swaroopch.com/control_flow.html)

## Loops

In our day-to-day lives and especially with our work we repeat ourselves. If your job is to clean a building, you have likely cleaned the same area (or areas that are similar) multiple times. If your job is to take phone calls, you may have had calls that felt exactly the same. Regardless of your work I am sure that you do things that sometimes become repetitive. The same thing happens in programming except that in programming we get to automate the process so we only have to 'say' it once. Loops are what make this possible. Python has different kinds of loops but the first one that we will learn about is called the `while` loop.

## The While Loop

While loops are a mechanism to repeat something *while* some condition is true. The [syntax](http://dictionary.reference.com/browse/syntax) for a `while` loop is very similar to an if statement. They both have a **keyword** followed by an **expression** that resolves to a **boolean** value. The first line is terminated by a **colon** and then the body of the loop is **indented**.

```python
while {boolean expression is true}:
    {do the things in the indented body}
```

A loop that counts to ten looks like:

```python
num = 1
while num < 11:
    print(num)
    num += 1
```

*Note*: the `+=` operator is a shortcut for `num = num + 1`.

Anything that resolves to a boolean `True` or `False` can be contained in the expression part of the loop. You can then put anything you wish to repeat inside the body of the loop. Imagine a loop that asks the user for various inputs based on their responses.

&#x20;*Try writing a program that keeps asking* *'Why?'* *until the user enters* *'just because!'*

## Infinite Loops

So imagine that you have a loop that looks like:

```python
happy = True
while happy:
    print("Life is just better this way!")
```

We set a variable `happy` equal to `True` and then never change it. Do you see the problem? We are never going to stop printing that statement. This is called an infinite loop because it repeats itself forever. If you find your program running in an infinite loop (I promise that you will write one eventually) you can terminate your program by pressing *Ctrl+C*. If you have ever tried to copy some text from the terminal you may have realized that the handy *Ctrl+C* shortcut didn't work. This is why. It is used to terminate your rogue programs.

Even though changing that `happy` variable to `False` seems ridiculous (who would ever want to), it is important to avoid an infinite loop. You will have to be careful when you write loops to avoid this pitfall.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://programming.reedcwilson.com/lesson-3/while-loops.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
