Loop (Unconditional)

icon picker
workshop tickets 3

calculate total seating for each person in every team using running total

requirement

The program should allow the user to enter the number of workshops running at a conference
the program should allow the user to enter the number of people attending each workshop
the program should add each group size to a running total

expected output

example output: 5 tickets
Enter number of workshops
3
How many students in workshop: 1
12
How many students in workshop: 2
13
How many students in workshop: 3
14
Total students: 39
you can see the total being updated in the output
the total will have been 0 before starting
the first group of 12 is added to total (0+12) to update total to 12
the second group of 13 is added to total (12+13) to update total to 25
the third group of 14 is added to total (25+14) to update total to 39

design

design : a line of code for one ticket nested within a loop
set the totalStudents to 0

prompt for the number of workshops
enter the number of workshops

loop for number of workshops with index
prompt for the number of students in workshop
enter the number of students

add the number of students to the totalStudents
end loop

display the totalStudents

learning-point

running total in a loop
a running total has 3 steps:
start total at 0,
update the total on each cycle by adding on a new value,
display the final value of total

introducing the code

1 System.out.println("How many workshops: ");
2 workshops = Integer.parseInt(keyboard.nextLine());

3 for (int i = 0; i < workshops; i++) {
4 System.out.println("How many students in workshop: " + (i + 1));
5 students = Integer.parseInt(keyboard.nextLine());

6 totalStudents = totalStudents + students;
7 }

8 System.out.println("Number of students: " + totalStudents);
line 1, 2 allows the user to enter the number of workshops and stores it in the variable workshops
line 3 sets up the loop control variable and uses the workshops variable to limit the cycles
lines 4-5 allow the user to enter another student group size
line 6 updates the total number of students by adding on the size of the latest group
line 8 displays the final total number of students

developer task

Run the example code in the program, enter test values for the workshop and look at the output.
Read the code. Try to recognise the names, types and values for each variable.
Identify the for-loop control structure.
Notice the loop control code in parenthesis ( ).
Notice the loop control variable: i,
Notice the nested instructions within the control structure in braces { }.
Compare the code with the output. Check out how the variables are used to display the ticket.
Notice the ticket number values now improved to: 1, 2, 3 .. (rather than : 0, 1, 2 .. )
This example will help you in the next exercise, so study it carefully!
read the comment lines in the program to develop similar code for the additional requirement described above
choose suitable test values to ensure that the new code works correctly

test values

test value 1: 3 (workshops), 12 (workshop1), 13 (workshop2), and 14 (workshop3)

the program code

Loading…
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.