IT 1090C Computer Programming I
Prof. Tom Wulf
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:
 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-2020, 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-2020, 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.
Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
 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
 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 itemShippingCosts
 Main()
 String itemsDone ""
 num itemCost = 0;
 num shippingCost = 0;
 num totalCost = 0;
 num totalWithShipping = 0;
 do
 Output "Please enter the price of one item $"
 Input itemCost
 totalCost = totalCost + iteamCost
 Output "Do you have more items [Y/N]: "
 Input itemsDone
 while itemsDone != "Y"
 if totalCost >= 100
 Output "You get free shipping! Your total is $" + totalCost
 else
 shippingCost = totalCost * .02
Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
 totalWithShipping = shippingCost + totalCost
 Output "Your total item cost is $" + totalCost + ". Your total with
 shipping is $" + totalWithShipping
 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;
 Output "Please enter your account balance $"
 Input balance
 Output "Please enter your intrest rate "
 Input interestRate
 for num month = 1; month <= 12; month = month + 1
 interest = balance * interestRate
 balance = intrest + balance
 Output "Your monthly balance is $" + intrest
Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
 endFor
 Return
endClass
 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.
Class studentGrades
 Main()
 num grade = 0;
 num gradeTotal = 0;
 num gardeCount = 0;
 num average = 0;
 do
 Output "Please enter a grade [0-100] if done enter [-1]: "
 Input grade
 if grade != -1 than
 gradeCount = gradeCount + 1
 gradeTotal = gradeTotal + grade
 endIf
 while (grade != -1)
 average = gradeTotal / gradeCount
Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
 Output "Your Garde average for your grades enterd is " + average
 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()
 String playA = ""
 String playB = ""
 String playAgain = ""
 do
 Output "Please enter Player A move: [R P S] "
 input playA
 Output "Please enter Player B move: [R P S] "
 input playB
 if playA == "R" then
 if playB == "R" then
 output "Rock vs Rock equals a TIE!"
 else if playB == "P" then
Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
 output "Paper covers Rock, Player B wins!"
 else // Player B nust be SCISSORS
 output "Rock breaks Scissors, Player A wins!"
 endIF
 else if playA == "P" than
 if playB == "R" then
 output "Paper covers Rock, Player A wins!"
 else if playB == "P" then
 output "Paper vs Paper equals a TIE!"
 else // Player B nust be SCISSORS
 output "Scissors cuts Paper, Player B wins!"
 endIF
 else // Player A must be SCISSORS
 if playB == "R" then
 output "Rock breaks Scissors, Player B wins!"
 else if playB == "P" then
 output "Scissors cut Paper, Player A wins!"
 else // Player B nust SCISSORS
 output "Scissors vs Scissors equals a TIE!"
 endIF
 end if
 Output "Do you want to play again [Y/N] : "
 Input playAgain
 while playA != "R" AND playA != "P" AND playA != "S" AND playB !=
Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
 "R" AND playB != "S" AND playB != "S" AND playAgain != "Y"
 Return
endClass
Extra Credit Options:
 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!
Class multiplicationTable
 Main()
 for num row =1; row <= 12; row = row + 1
 for num col = 1; col <= 12; col = col + 1
 Output row * col
 endFor
 Output NEWLINE
 endFor
 Return
endClass
Copyright © 2019-2020, University of Cincinnati, Ohio. All rights reserved.
 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-2020, University of Cincinnati, Ohio. All rights reserved.