# Python while Loop

Updated: Aug 21, 2020

In the last lecture we discussed about for loops , now we will cover while loops.

The while statement in Python is one of most general ways to perform iteration. A while statement will repeatedly execute a single statement or group of statements as long as the condition is true. The reason it is called a 'loop' is because the code statements are looped through over and over again until the condition is no longer met.

The general format of a while loop is:

```
while test:
code statements
else:
final code statements
```

Let’s look at a few simple while loops in action.

**Example:**

```
x = 0
while x < 10:
print('x is currently: ',x)
print(' x is still less than 10, adding 1 to x')
x+=1
```

Output:

```
x is currently: 0
x is still less than 10, adding 1 to x
x is currently: 1
x is still less than 10, adding 1 to x
x is currently: 2
x is still less than 10, adding 1 to x
x is currently: 3
x is still less than 10, adding 1 to x
x is currently: 4
x is still less than 10, adding 1 to x
x is currently: 5
x is still less than 10, adding 1 to x
x is currently: 6
x is still less than 10, adding 1 to x
x is currently: 7
x is still less than 10, adding 1 to x
x is currently: 8
x is still less than 10, adding 1 to x
x is currently: 9
x is still less than 10, adding 1 to x
```

Notice how many times the print statements occurred and how the while loop kept going until the True condition was met, which occurred once x==10. It's important to note that once this occurred the code stopped. Let's see how we could add an else statement:

**Example:**

```
x = 0
while x < 10:
print('x is currently: ',x)
print(' x is still less than 10, adding 1 to x')
x+=1
else:
print('All Done!')
```

Output:

```
x is currently: 0
x is still less than 10, adding 1 to x
x is currently: 1
x is still less than 10, adding 1 to x
x is currently: 2
x is still less than 10, adding 1 to x
x is currently: 3
x is still less than 10, adding 1 to x
x is currently: 4
x is still less than 10, adding 1 to x
x is currently: 5
x is still less than 10, adding 1 to x
x is currently: 6
x is still less than 10, adding 1 to x
x is currently: 7
x is still less than 10, adding 1 to x
x is currently: 8
x is still less than 10, adding 1 to x
x is currently: 9
x is still less than 10, adding 1 to x
All Done!
```

**break & continue keywords**

We can use break, continue, and pass statements in our loops to add additional functionality for various cases. The two statements are defined by:

```
break: Breaks out of the current closest enclosing loop.
continue: Goes to the top of the closest enclosing loop.
```

The **continue** statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

**Example:**

```
for letter in 'Python':
if letter == 'h':
continue
print('Current Letter :', letter)
```

Output:

```
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
```

**Example:**

```
var = 10
while var > 0:
var = var -1
if var == 5:
continue
print('Current variable value :', var)
print("Good bye!")
```

Output:

```
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!
```

The **break** statement in Python terminates the current loop and resumes execution at the next statement

**Example:**

```
for letter in 'Python':
if letter == 'h':
break
print ('Current Letter :', letter)
```

Output:

```
Current Letter : P
Current Letter : y
Current Letter : t
```

**Example:**

```
var = 10
while var > 0:
print('Current variable value :', var)
var = var -1
if var == 5:
break
print("Good bye!")
```

Output:

```
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!
```

After these brief but simple examples, you should feel comfortable using while statements in your code.

**A word of caution however! It is possible to create an infinitely running loop with while statements.**

**Example:**

```
# DO NOT RUN THIS CODE!!!!
while True:
print("I'm stuck in an infinite loop!")
```

That is it for this article, next article we will read about range . Till then relax and sip a coffee :-)