Loop (Unconditional)

icon picker
workshop tickets 1

create multiple copies of a ticket for an event by using a for-loop

requirement

The program should allow the user to enter the name of a workshop event at a conference
the program should then create 5 tickets which include the name of that workshop

expected output

example output: 5 tickets
Which workshop are you going to
Cryptography
- - - - - - - - - - - - - - -
Interrupt19 - Cyber Conference
Workshop: Cryptography
Student Admission
ticket number: 0
- - - - - - - - - - - - - - -

- - - - - - - - - - - - - - -
Interrupt19 - Cyber Conference
Workshop: Cryptography
Student Admission
ticket number: 1
- - - - - - - - - - - - - - -

...

- - - - - - - - - - - - - - -
Interrupt19 - Cyber Conference
Workshop: Cryptography
Student Admission
ticket number: 4
- - - - - - - - - - - - - - -

learning-point

loop code
a for-loop in java will repeat the code nested within the loop a fixed number of times (cycles)
only the code nested within the loop is repeated, the code before and after the loop is unaffected
the java loop counts from 0 up to the number of required cycles
a java for-loop uses a loop control variable called i, say “index”, it is an integer variable
the loop control variable stores the count value which increments on each cycle
the value of the loop control variable can be output during the loop

design

design 1: a line of code for each ticket
enter workshop name

display ticket for workshop
display ticket for workshop
display ticket for workshop
display ticket for workshop
display ticket for workshop
design 2: a line of code for one ticket nested within a loop
enter workshop name

repeat 5 times
display ticket for workshop
end repeat

introducing the code

1 for (int i = 0; i < 5; i++) {
2 System.out.println("- - - - - - - - - - - - - - -");
3 System.out.println(" Interrupt19 - Cyber Conference");
4 System.out.println(" Workshop: " + workshop);
5 System.out.println(" Student Admission");
6 System.out.println(" ticket number: " + i);
7 System.out.println("- - - - - - - - - - - - - - -");
8 System.out.println();
9 }
line 1 begins the loop and sets up the loop control variable: i
line 1 i starts count the loop cycles at 0 (i=0) and increase by 1 on each cycle (i++)
line 1 i stops as i reaches 5 so it counts: 0, 1, 2, 3, 4 and then stops, so it cycle 5 times
lines 2-8 create 1 ticket
line 4 includes the workshop name entered by the user
line 6 displays the value of the loop control variable, this will increment on each cycle of the loop
line 9 is the end of the loop, flow of control goes back to line 1 to cycle again or to stop

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: 0, 1, 2, 3, 4 (rather than : 1, 2, 3, 4, 5), you can improve this later)
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: cryptography (workshop)

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.