PROGRAMMING 1A
(PROG5121)
CREATING LOOPS
BASED ON CHAPTER 6, FARRELL, J. 2019. JAVA PROGRAMMING.
9TH ED. 2019. COURSE TECHNOLOGY, CENGAGE LEARNING.
THE LOOP STRUCTURE
• A loop is a structure that allows repeated execution of a block of statements.
• Within a looping structure, a Boolean expression is evaluated:
If it is true, a block of statements (called the loop body) executes, and the Boolean
expression is evaluated again.
If it is false, the block of statements is not executed, and processing continues after the
block.
• One execution of any loop is called an iteration.
• The commonly used loops in Java are the following three:
The while loop, in which the loop-controlling Boolean expression is the first statement in
the loop.
The for loop, which is usually used as a concise format in which to execute loops.
The do…while loop, in which the loop-controlling Boolean expression is the last statement
in the loop.
SHORTCUT ARITHMETIC OPERATORS
• Java provides several shortcuts for incrementing and accumulating numbers that are often used
with loop structures:
count += 10 is identical to: count = count + 10.
count -= 10 is identical to: count = count - 10.
count *= 10 is identical to: count = count * 10.
count /= 10 is identical to: count = count / 10.
count %= 10 is identical to: count = count % 10.
• When you want to increase a variable’s value by exactly 1, you can use two other shortcut
operators:
The prefix ++ (also known as the prefix increment operator)
b = 4; c = ++b; // With prefix method: b = 5 and c = 5
The postfix ++ (also known as the postfix increment operator)
b = 4; c = b++ // With postfix method: b = 5 and c = 4
THE WHILE LOOP
THE FOR LOOP
The for loop rewritten as a while
loop.
THE 'DO..WHILE' LOOP
• The do…while loop executes
a loop body at least one time.
• It checks the loop control
variable at the bottom of the
loop after one repetition has
occurred; e.g.:
do {
total += numberValue;
} while(total < 200);
NESTED LOOPS
for(customer = 1; customer <= 20; ++customer)
for(color = 1; color <= 3; ++color)
OutputLabel(); // Print 3 labels per
customer.
IMPROVING LOOP PERFORMANCE
• while(x < a + b) {…}
This should be avoided because 'a + b’ is recalculated for every loop iteration.
• while(requestedNum > LIMIT || requestedNum < 0) {…}
This statement has a first Boolean expression that's more likely to occur than the second because
OR uses short circuit evaluation and can therefore improve performance.
• Comparing a value to 0 instead of other values is faster because in a compiled language,
condition flags for the comparison are set once, no matter how many times the loop executes.
• Comparing a value to 0 is faster than comparing to other values, no matter which comparison
operator you use - greater than, less than, equal to, and so on.
• Loop fusion is the technique of combining two loops into one and can improve performance.
• Use prefix incrementing rather than postfix incrementing because postfix makes a copy of the
value and stores it, increments the value indicated by the reference, and returns the copy.
• Prefix simply receives a referenced value, increments it, and returns it.
ICE TASK 4
• In general, elapsed time is the time from the starting point to the ending point of an event. Following
are various ways to find elapsed time in Java. The program you’re given shows the following ways:
The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a
method you can get the difference between time values before and after the execution of the desired method.
The nanoTime() method returns the current time in nano seconds. To find the elapsed time for a method you
can get the difference between time values before and after the execution of the desired method.
The now() method of the Instant class returns the current start and end times, and the Duration.between()
method returns the difference between the these time values to get the elapsed time.
The Apache commons library provides a class known as Stopwatch which provides a start(), stop() and
getTime() methods to find the time taken for the execution of a method (use Maven to download the library).
• Your task is to modify the given ExecutionTime class in the program, so that the for loop is replaced
by a while loop of symmetric logic (this means that the while loop must give the same output as the
for loop). You can do this by adding a method called testWhileLoop() and calling it in the same class.
• Once you get your while loop version running correctly, compare and comment on the performance
between the two kinds of loop by running them. Which one is faster, the for loop or the while loop?