While Loop and Do While Loop

Brian E. Lavender

December 17, 2014

While Loop flow chart diagram

While loop flowchart

While loop flowchart

While Loop pseudocode

While condition
    statement
    statement
    etc.
End While

Note

condition must be made to evaluate true for first loop.

Conditions

    keepGoing == "y"
    x < 3
    x < 3 && keepGoing == "y"

Sample 1 While Loop

Declare Integer x = 3
While x > 1
    Display "Hi There ", x
    Set count = count - 1
End While

Output

Hi There 3
Hi There 2
etc.

What do you notice that we put before the while loop?

Sample 2 While Loop

Declare String doAgain = "Y"
While doAgain == "Y"
    Display "Hi There"
End While

Output is an infinite loop!

Hi There
Hi There
etc.

How would you fix this?

Do While Loop flow chart diagram

Do While flowchart

Do While flowchart

Do While Loop pseudocode

Do
    statement
    statement
    etc.
While condition

Sample 1 Do While Loop

Declare Integer x = 3
Do
    Display "Hi There ", x
    Set count = count - 1
While x > 1

Output is the same as the while loop!

Hi There 3
Hi There 2

Sample 2 Do While Loop

Declare String keepGoing

Do
    Display "Hi There "
    Input keepGoing
While keepGoing == "y"

Notice how keepGoing is not preinitialized.

Hi There
y
Hi There
n

Sample 3 Do While Loop

Declare Integer x = -1
Do
    Display "Hi There ", x
    Set count = count - 1
While x > 1

Output

Hi There -1

What happened? Why did the number go negative?

While Do Big Task

While Do big task

While Do big task

pseudocode for Do While Big Task

// Variable declarations
Declare Real sales, commission
Declare String keepGoing = "y"
// Constant for the commission rate
Constant Real COMMISSION_RATE = 0.10
While keepGoing == "y"
    // Get the amount of sales.
    Display "Enter the amount of sales."
    Input sales
    // Calculate the commission.
    Set commission = sales * COMMISSION_RATE
    // Display the commission
    Display "The commission is $", commission
    Display "Do you want to calculate another"
    Display "commission? (Enter y for yes.)"
    Input keepGoing
End While

Breaking into a module

While Loop with a Module

While Loop with a Module

Module pseudocode

Module main()
    // Local variable
    Declare String keepGoing = "y"
    
    // Calculate commissions as many
    // times as needed.
    While keepGoing == "y"
        // Display a salesperson's commission.
        Call showCommission()
        // Do it again?
        Display "Do you want to calculate another"
        Display "commission? (Enter y for yes.)"
        Input keepGoing
    End While
End Module

// The showCommission module gets the
// amount of sales and displays the
// commission.
Module showCommission()
    // Local variables
    Declare Real sales, commission
    // Constant for the commission rate
    Constant Real COMMISSION_RATE = 0.10
    // Get the amount of sales.
    Display "Enter the amount of sales."
    Input sales
    // Calculate the commission.
    Set commission = sales * COMMISSION_RATE
    // Display the commission
    Display "The commission is $", commission
End Module

Exercise

Write the previous While loop using a Do While loop

Questions?!