These projects were created by Leigh Anne Fitz and Lisa Ferran to provide engaging, standards-aligned learning experiences for AP Computer Science A students.
Now that your programs can make decisions, itβs time to make them repeat those decisions efficiently. In these projects, youβll explore how loops allow programs to execute instructions multiple times without writing the same code over and over. Youβll use iteration to solve problems, process data, and build algorithms that are both efficient and easy to maintain.
Each project is designed to reinforce these concepts through authentic programming tasks. As you complete them, focus on recognizing patterns, choosing the appropriate type of loop, and tracing your algorithms to ensure they repeat the correct number of times. Great programmers know that well-designed loops make programs more powerful, flexible, and efficient.
Subsection2.32.1Review: Using for Loops to Repeat Code
A for loop is used when you know how many times you want a section of code to repeat. The loop uses a counter variable that keeps track of the number of repetitions. The general structure of a for loop is:
Write a program that allows the user to input the number of grades he or she wants to be averaged. Then use a for loop structure to have the user input all the test grades individually, calculating the sum of these grades as they are being entered. These amounts, including the sum, are to be all ints. After all the grades have been entered, the program will calculate the average (what data type should this variable be?). Print out the average for the number of grades entered.
A while loop repeats a section of code as long as a condition is true. While loops are useful when you do not know exactly how many times a loop will repeat.
This condition only runs when the user enters the stopping value. The sentinel value should end the loop, not continue it.
while (number != -1)
The loop should continue while the user has not entered the sentinel value. Since -1 indicates that the user wants to stop, the condition should check: number != -1. The loop will repeat for all other numbers and stop when number becomes -1.
while (number < -1)
Consider whether all valid inputs will be less than -1. The loop should continue for most values except the sentinel.
while (number > -1)
This condition would prevent the loop from running for many possible valid inputs. Think about what value should cause the loop to stop.
This condition is true for valid input. A validation loop should repeat when the input is incorrect.
while (number == 0)
This only catches one invalid value. What about negative numbers?
while (number <= 0)
The loop should continue while the input is invalid. A positive number must be greater than 0, so values that are invalid are: 0 and Negative numbers. The condition: while (number <= 0) keeps asking for input until the user enters a valid positive number.
while (number != 0)
This condition would reject all nonzero numbers, including valid positive numbers.
The String.format() method can be used to display a double value with a specific number of decimal places. To format a number to two decimal places, use: String.format("%.02f", number)
Recall the Concert Tickets program from Unit 2A where the user chooses a seat based on the location with the costs listed below (remember that these values should be used as ints).
Allow the user to continue buying tickets (one at a time). At the end of the loop, ask the user if they would like to purchase another ticket. Y for yes, and N for No.
When prompting the user for a ticket type, if the user inputs an invalid type, use a loop to prompt the user to keep entering a seat type until a valid input is received.Β This is to be done with a while data verification loop.
Keep a running total of the number of each type of ticket purchased and the subtotals for the costs of each type of ticket (these will need to be stored as ints)
After all tickets have been purchased, display the quantity and type of each ticket bought with a subtotal, the total convenience fee, and a total price for all the tickets.
*Be sure to put a $ in front of all monetary amounts, and be sure to use String formatting on the convenience fee and total so that you will ensure two decimal places.
Modify and expand the Unit 2A Concert Tickets program to process multiple ticket orders iteratively. Implement a loop allowing the user to purchase tickets one at a time until they choose to stop, enforce input validation for seat selections using a while loop, track counts and subtotals for each ticket type, apply a per-ticket convenience fee, and present an itemized final receipt with properly formatted currency values.