LAB 4: LOOPS.

Loops are constructs that control repeated executions of a block of statements. The concept of looping is fundamental to programming. Java provides three types of loop statements: while loops, do-while loops, and for loops.

LAB SHEET 4.1: The while Loop

ESTIMATED TIME / : / 45 minutes
OBJECTIVE / : / To write program for executing statements repeatedly using a while loop.
REQUIREMENTS / : / JCreator 5.00 Pro
PROCEDURE / : / Read the question and write the program. Then run the program and get the correct output.
CONCLUSION / : / The while loop checks the loop-continuation-condition first. If the condition is true, the loop body is executed; if it is false, the loop terminates.

DISCUSSION / RESULT / EXERCISE

(Conversion from miles to kilometers) Write a program that displays the following table (note that 1 mile is 1.609 kilometers):

Miles Kilometers

1 1.609

2 3.218

...

9 14.481

10 16.090

LAB SHEET 4.2: The do-while Loop

ESTIMATED TIME / : / 45 minutes
OBJECTIVE / : / To write loops using do-while statements.
REQUIREMENTS / : / JCreator 5.00 Pro
PROCEDURE / : / Read the question and write the program. Then run the program and get the correct output.
CONCLUSION / : / The do-while loop is called posttest loop because the condition is checked after the loop body is executed.

DISCUSSION / RESULT / EXERCISE

You have three identical prizes to give away and a pool of 10 finalists. The finalists are assigned numbers from 1 to 10. Write a program to randomly select the numbers of 3 finalists to receive a prize. Make sure not to pick the same number twice. For example, picking finalists 3, 6, 2 would be valid but picking 3, 3, 11 would be invalid because finalist number 3 is listed twice and 11 is not valid finalist number. Random number generation is not discussed in this course, but for this problem you can insert the following line of code to generate a random number between 1 and 10:

int num = (int) (Math.random() * 10) +1;

LAB SHEET 4.3: The for Loop

ESTIMATED TIME / : / 45 minutes
OBJECTIVE / : / To write loops using for statements.
REQUIREMENTS / : / JCreator 5.00 Pro
PROCEDURE / : / Read the questions and write the program. Then run the program and get the correct output.
CONCLUSION / : / The for loop generally is used to execute a loop body a predictable number of times; the number is not determined by the loop body.

DISCUSSION / RESULT / EXERCISE

(Displaying the ASCII character table) Write a program that prints the characters in the ASCII character table from ‘!’ to ‘~’. Print ten character per line. The ASCII table is shown in Appendix B.

LAB SHEET 4.4: Nested Loops

ESTIMATED TIME / : / 45 minutes
OBJECTIVE / : / To write nested loops.
REQUIREMENTS / : / JCreator 5.00 Pro
PROCEDURE / : / Read the question and write the program. Then run the program and get the correct output.
CONCLUSION / : / The nested for statement is used very often because it is ideally suited to process tabular data.

DISCUSSION / RESULT / EXERCISE

(Math: combinations) Write a program that displays all possible combinations for picking two distinct numbers from integers 1 to 9:

1 2

1 3

...

9 7

9 8

1