While Statement - Python (2016)

Python (2016)

CHAPTER 12: While Statement

While the if statement we learned in the previous lesson can be applied for different purposes, it does not do quite as well a job at being recursive. This means that this statement is not made to loop multiple times. At this point, the while statement comes into play. This statement will work for as long as it needs to, until the conditions are proven false. This is useful in case something has to be repeated multiple times for a specific amount of time. Below is an example that demonstrates this:

>>>spam= 0

>> >while spam !=5:

... Spam +=1

... Print (spam)

...

1

2

3

4

5

>>>bacon=[2,4,5,6,8]

>>>while bacon:

... Print(bacon [0])

... Del bacon[0]

...

2

4

6

8

>>>while False:

... Print(“This will not print”)

...

>>> juice=10

>>>while juice:

... juice-=1

... print(juice)

...

9

8

7

6

5

4

3

2

1

0

The condition of the while statement will always be true or false, just like the if statement. This will mean that Booleans can also be used to force the looping. In the same vein, while False: will not execute—in the opposite end, while True: will loop infinitely. This will be important in more advanced lessons.

The Else Statement

In Python, unlike in many other languages, the else statement can be legally used in conjunction with the while statement. When the while finishes the loop naturally, the else statement will kick in. If the while statement is prematurely stopped, then the else statement will not execute. Below is an example:

>>>jam=0

>>>while jam !=10:

... jam+=1

... Print(jam)

...else:

... Print(“Finished printing jam”)

...

1

2

3

4

5

6

7

8

9

10

Finished printing jam

>>>while True:

... Pass

...else:

... Print(“This will not be seen on the screen”)

...

Traceback (most recent call last):

File “<stdin>”, line 2, in <module>

KeyboardInterrupt

In the second batch of code, the KeyboardInterrupt was generated by a ctrl+c, Like mentioned earlier, the while statement was prematurely stopped and hence the else was not executed.

The Break Statement

Sometimes, the programmer may wish to intentionally end a while statement prematurely. This is done through the break statement. This statement is nested in the while statement that it will end. This means that only one while statement will be ended, and not all of them. Below is an example:

>>>spam=0

>>>while True:

... Print(spam)

... Spam +=1

... If spam ==10:

... Break

...

0

1

2

3

4

5

6

7

8

9

This example shows how a while loop is ended, even while it is true. You will also notice that the break statement we demonstrated did not need a colon. This means a new indentation is not needed. Placing the colon will cause an error—meaning this omission is mandatory.

Continue Statement

Just as the break statement can be useful, so can the continue statement. Unlike the previous statement, continue will stop the current code execution and proceed to the beginning of the while statement. This is used to skip a part of your code without ending the loop. This statement is used just like the break statement. Here is an example:

>>>eggs=0

>>>while eggs!=10:

... eggs+=1

... If eggs <5:

... Continue

... Print (eggs)

...

5

6

7

8

9

10

Astute readers will notice that the example above could have been complete even without the continue statement. It does, however, demonstrate exactly how the statement works. It is used to reduce the need for indenting and nesting. It would be a bit easier to check the five conditions and usecontinue when they fail. This is in comparison to nesting five if statements. This can also reduce the time needed for typing, also directly affecting the clarity and readability of the code.