Lab 03 Looping Spring 2022 v1
Lab 03 Looping Spring 2022 v1
Lab 3 Looping
15 pts / 6 extra
Mini-lecture:
We will continue to refine our pseudo code. Now, we will look at looping
We looked at looping in class. Java has a while loop and a for loop. These are both pre-test
loops. The for loop is a definite loop and the while loop is an indefinite loop. (Java also has a
post-test indefinite loop: do..while.
Here is the flowchart and the pseudo code for an indefinite loop that uses a sentinel to terminate
the repetition of the loop. In this variation the sentinel is the value “N” which the user enters
when they are prompted if they want to continue or not.
Here is the general format for the while loop in pseudo code:
As discussed, Boolean expressions are expressions that evaluate to true or false. These are
formed by relational operators (<. >,< =,>=, !=) and the logical operators AND && and OR ||.
For loops are definite pre-test loops. Use these when you want to repeat something some fixed
number of times.
Which to use?
Use a while loop when you don’t know exactly how many times the loop will repeat (indefinite
loop)
Use a for loop when you know exactly how many times the loop will repeat (definite loop)
Both these loops are pre-test loops which means that if the first test fails (is false) then the
statements in the loop body are never executed.
The do while loop is also indefinite but is post test so that loop runs the code block at least once
before the first test. Every time you get input you do this. You collect data and plan to loop back
only to correct it!
Lab:
1. Just submit this document (See directions below.) and insert your work after each task
description.
2. For each of the following tasks, provide the complete pseudo code including all
elements that we have been using (i.e. class, end class, main return, output prompt).
Be sure to declare your variables and use symbolic constants where appropriate.
Determine if each task requires a definite or indefinite loop and use the correct one.
Use the do while loop for loops that you want to execute at least once, like getting
input.
Class itemShippingCosts
Main()
num itemCost = 0;
num shippingCost = 0;
num totalCost = 0;
num totalWithShipping = 0;
do
Input itemCost
Input itemsDone
else
Output "Your total item cost is $" + totalCost + ". Your total with
endIf
Return
endClass
b. Task 2 (4 pts): A program that prompts the user for an initial balance
and a monthly interest rate and then displays each new monthly balance
for a 12 month period. (Interest is compounded. Print inside the loop so
you display each of the 12 balances not just the final.)
Class monthlyIntrestRate
Main()
balance = 0;
intrest = 0;
interestRate = 0;
Input balance
Input interestRate
Return
endClass
Class studentGrades
Main()
num grade = 0;
num gradeTotal = 0;
num gardeCount = 0;
num average = 0;
do
Input grade
if grade != -1 than
gradeCount = gradeCount + 1
endIf
Return
endClass
d. Task 4 (5 pts): Rock Paper Scissors: The game repeats as long as the user
enters Y to a prompt to continue. Get a move from player A and then
from player B. Assume that the move is valid so it has to be one of the 3
choices (R P or S). Your program should compute the winner with an
appropriate output string: “Rock Breaks Scissors Player A wins, etc...
And then prompt to play again…
Class rockPaperScissors
Main()
do
input playA
input playB
endIF
endIF
endIF
end if
Input playAgain
while playA != "R" AND playA != "P" AND playA != "S" AND playB !=
Return
endClass
e. Task 5 (4 pts): Code a number guessing game. The user picks a number
between 1 and 10 and the computer ‘guesses’ the value. Each time, the
player indicates if the guess is larger or smaller by entering a plus or a
minus sign. If the computer guesses the value correctly then the user
enters a exclamation point !. Assume in this case that the player/user it
truthful.
Class multiplicationTable
Main()
endFor
Output NEWLINE
endFor
Return
endClass