PAT - While Loop (Lesson)

While Loop

while condition is true

While it is Raining (next line) I will carry an umbrella

Infinite Loop

This loop, the while loop, will continue until itIsRaining changes to false, which is 0. So where does it change to false? It doesn't unless the programmer takes control. If the programmer does not take control, the code above creates what is known as an infinite loop.

Infinite loop is a loop condition that has no end.

An infinite loop does not end until the program is forcibly stopped running or memory in the computer runs out whichever is first. This is not pretty and no one wants their program to crash. Below is the control structure needed to prevent the infinite loop in the code above.

In this case there would need to be code for determining whether rain was still continuing. The while loop requires the programmer to intervene to stop the loop.

Hare and Monkey While Loop Construct

For a change of pace, let's look at a while loop construct using our Hare & Monkey prior to evaluating these concepts by hand.   Let's understand first how the while loop works.

Adding Control Concept

Now, let's add a control concept to the while loop in the following Hare and Monkey video. When we accept information from a user, we must check what is entered into the program to avoid the runtime error discussed in the debugging topic, a program running on and on because the program is in a looping too long or stuck in an infinite loop. Watch the video below to learn the concepts of basic user control.  

Now it is time to return to looking at more complex mathematical concepts with loops. Look at this while loop with numbers.

a = 0

cnt= 1

while cnt <= 5

a=a +2

cnt=cnt + 1

Note the control here is the variable cnt. While cnt is less than 5 or equal to 5, the loop continues. When cnt is greater than 5, the loop condition cnt <= 5 is no longer true, so the loop exits.  

So what does this loop do and what is the result, the answer?            

We will now trace the loop. This is an important concept in programming. Tracing means that you evaluate step by step what the program is doing.  

a is assigned the value of 0

cnt is assigned the value of 1

now evaluate the while loop and keep track.

 

while cnt <=5 // while the count is less than or = to 5 do the loop

a =a + 2

cnt = cnt + 1

 

So now we trace the loop. Create a table to keep track.

a

cnt

Is cnt <= 5?

a = a + 2

cnt = cnt + 1

0

1

yes

a = 0 + 2 = 2

cnt =     1 + 1 = 2

2

2

yes

a = 2 + 2 = 4

cnt =     2 + 1 = 3

4

3

yes

a = 4 + 2 = 6

cnt =     3 + 1 = 4

6

4

yes

a = 6 + 2 = 8

cnt =     4 + 1 = 5

8

5

yes

a = 8 + 2 = 10

cnt =     5 + 1 = 6

10

6

No, exit loop to the statement after the loop

 

 

Test Yourself

Consider the following questions about this while loop. Check your answers by clicking on the answer document at the end of the questions.

What is the result for the variable "a" in the while loop?  

What is the result at the end of the while loop for the variable cnt?      

How many times was the loop executed?

Using this code:

a = 0

cnt = 1

while cnt >= 5

        a = a + 2

        cnt = cnt + 1

What is the result of the while loop?  

What is the result of the while loop using the code below?

a = 0

cnt = 1

while cnt > 0

      a = a + 2

      cnt = cnt + 1

Lastly, explain what happens with this code.

a = 0

cnt = 1

while cnt <= 5

        a = a + 2

cnt = cnt + 1

Click here to check your answers! Links to an external site.

All infinite loop runtime errors must be guarded against by the programmer.  Always test carefully and read your indentations.  Indentations are ways of grouping code together to create a body of statements that apply to a loop, the if side or else side of an if-else, or other control and decision statements, including count as we saw in the earlier videos.

Did you notice the statements a = a + 2 and cnt = cnt + 1? In math, this would not balance as a math statement as the right side must match the left side. In programming, since the equal, =, statement means assign, it is legal. The answer evaluation from the right side of the equation is assigned to the left side variable. Perfectly legal do to the definition of = computer science.

Abstract process in the while loop

  • Loop construction
  • Rechecking the condition
  • On false going to the next statement after the loop

IMAGE CREATED BY GAVS AND USED ACCORDING TO TERMS OF USE.