You are on page 1of 8

IT 1090C Computer Programming I

Lab 3 Looping
SPRING 2020
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:
while BOOLEAN EXPRESSION
statements here that will be repeated
endWhile
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 ||.

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.


For loops:

For loops are definite pre-test loops. Use these when you want to repeat something some fixed
number of times.

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.


Pseudo code for the for loop:

for count = 1 to 5 step 1


these statements repeat
endFor

Pseudo code for the do .. while loop

do
these statements repeat
while BOOLEAN EXPRESSION

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.

a. Task 1 (3 pts): an application program where the user enters the price of
a series of items (assume at least one.) (Prompt user for additional items

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.


until they are done, so we don’t know ahead how many items there will
be. This is the looping part! ;) ) The program computes shipping costs.
If the total price of all items is $100 or more, then shipping is free
otherwise it is 2% of the price. The program should output the total price
of all items, the shipping cost, and the total cost.

class ShippingCalculator
main()
num itemCost = 0
num itemTotal = 0
num SHIPPING_RATE = .02
num shippingTotal = 0
num billTotal = 0
num itemNumber = 0
String response
boolean done = false
do
itemNumber = itemNumber + 1
output “Please enter the cost of item number” + itemNumber
input itemCost
itemTotal = itemTotal + itemCost
output “Do you have another item? Type Y for yes and N for no”
input response
if response == “N”
done = true
while done == false
If itemTotal <= 100 then
shippingTotal = itemTotal * SHIPPING_RATE
billTotal = shippingTotal + itemTotal
endIf
output “The total cost of your items is $” + itemTotal + “ The total cost of your shipping is
$” + shippingTotal + “ The total cost of your bill is $” + billTotal
return
end class

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

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.


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 MonthlyInterestRateCalc
main()
num initialBalance = 0
num interestRate = 0
num interestTotal = 0
num month = 0
output “What is your initial balance?”
input initialBalance
output “What is your monthly interest rate?”
input interestRate
for month = 1 to 12 step 1
interestTotal = initialBalance * interestRate
initialBalance = initialBalance + interestTotal
interestTotal = 0
output “Your balance after “ + month + “ month(s) is $” + initialBalance
endFor
return
end class

c. Task 3 (3 pts): A program that collects a series of grades (ranging 0 –


100) until the user indicates that they don’t want to continue entering
values useing a sentinel value. -1 is a good choice for a sentinel here.)
The program should display the average of the grades entered.

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.


class GradeAverage
main()
num gradeValue = 0
num gradeTotal = 0
num gradeCounter = 0
gradeAverage = 0
boolean done = false
output “Please enter a grade score between 0 – 100. To finish enter any invalid number”
do
input gradeValue
if gradeValue >= 0 AND <= 100 then
gradeTotal = gradeTotal + gradeValue
gradeCounter = gradeCounter + 1
else
done = true
while done = false
gradeAverage = gradeTotal / gradeCounter
output “The average grade is “ + gradeAverage
return
end class

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()
string playerA
string playerB
boolean done = false

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.


string response
while done = false
output “Player A please enter R for rock, P for paper, or S for scissors.”
Input playerA
Output “Player B please enter R for rock, P for paper, or S for scissors.”
Input playerB
If playerA == “R” then
If playerB == “R” then
output “It’s a tie!
elseIf playerB == “P” then
output “Paper covers Rock. Player B wins!”
else
output “Rock breaks Scissors. Player A wins!
endIf
If playerA == “P” then
If playerB == “R” then
output “Paper covers Rock. Player A wins!”
elseIf playerB == “P” then
output “It’s a tie!”
else
output “Scissors cuts Paper. Player B wins!”
endIf
If playerA == “S” then
If playerB == “R” then
output “Rock breaks Scissors. Player B wins!”
elseIf playerB == “P” then
output “Scissors cuts Paper. Player A wins!”
else
output “It’s a tie!”
endIf
endIf
Output “Would you like to continue? Y for yes”
Input response
If response == “Y” then
Done = true
endWhile
return
end class

Extra Credit Options:

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.


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.

f. Task 6 (2 pts): A program that displays the 12 by 12 multiplication table.


This is a bit of a challenge as it requires a nested loop so I’ll grant you
extra credit if you complete it!

3. Submitting your work: carefully check your work. Rename your word file as
Lastname_Firstname_Lastname_Firstname Lab03.docx using your name.
Submit this file using the Canvas assignment mechanism. For The Extra Credit
resubmit the entire lab again using the second submission link provided.

Copyright © 2019, University of Cincinnati, Ohio. All rights reserved.

You might also like