Inf1 OP : Lab Sheet Week 2 Q9 - Squares Loop
Overview
Warning
Pair Programming:
This exercise is reserved for pair programming in the live lab sessions.
Please skip it when doing the exercises individually.

For our first loop we’re going to start off simple. Your task is to print out a list of the squares of an integer, as shown below:

1 4 9 16 25 36 49 64 81 100
Introduction to for loops

We’ll do this using a for loop. For loops have three main components. In general they will have their own variable to keep track of which iteration of the loop is being executed — we will sometimes call this the loop variable. They also have a boolean condition, the same as with the if-then-else statement, which is checked at the start of every loop iteration. If the operating condition is true then another iteration will be executed, otherwise the loop terminates and the rest of the program is allowed to execute. Finally, for loops have an update statement which normally updates the loop variable, say incrementing it, at the end of every iteration. The syntax of a for loop is shown below:

for ( initialization; boolean condition; iteration statement ) {
        // loop body
}
// instructions to be executed after the loop has finished

For example, if we wanted to do something twenty times — say print a * to the terminal — we could use a for loop. The loop variable would just be a counter that would correspond to how many times the loop has executed. We initialise this variable to zero as it hasn’t executed yet. The boolean condition would be set to check that the loop variable is less than 20, and the iteration statement would increment the counter. The for loop we would use is shown below:

for ( int count = 0; count < 20; count++ ) {
        // loop content
}

Note

The statement count++; is equivalent to count = count + 1;. Since incrementing a variable by one is such a common action in programming, the ++ notation is a useful shorthand in many languages.

In a complete program, the for loop will be placed inside a main() method, as illustrated here:

public class Repeat20 {

    public static void main(String[] args) {
        for (int count = 0; count < 20; count++) {
            System.out.print("*");
        }
        System.out.println("!");
    }
}

Figure out what output this program will produce.

We are also able to access the count variable within the loop. For example, if we wanted to print out which iteration the loop is on, we could add the following statement inside the loop body:

System.out.println(count);

Now write a program SquaresLoop which uses a for loop to display the square of the numbers from 1 to 10, as shown below. Don’t forget to print out a new-line after the loop has terminated.

:java SquaresLoop
1 4 9 16 25 36 49 64 81 100

An automated test has been created for this exercise: SquaresLoopTest.java.

Change of Range

How would you change your loop to print just the squares between a specified range, say between 10 and 20?

Write a program SquaresLoopRange which takes two command-line arguments, a start-number and a stop-number, and prints the squares between these numbers inclusively, (i.e. including the squares of the start-number and stop-number themselves). For example:

:java SquaresLoopRange 10 20
100 121 144 169 196 225 256 289 324 361 400

:java SquaresLoopRange 1 10
1 4 9 16 25 36 49 64 81 100

Your program should handle the case when the start-number is larger than the stop-number by displaying the following error message:

:java SquaresLoopRange 10 1
Start-limit greater than stop-limit!

An automated test has been created for this exercise: SquaresLoopRangeTest.java.

Just the Multiples

Write a third program, MultiplesLoopRange, which prints out numbers in a certain range, provided that they are a multiple of a certain value. (Note that we are no longer interested in squaring the values.) Your program should take three command-line arguments, representing the start number, the end number, and the required factor. For example, if we want to print out all numbers between 20 and 30 which are multiples of 3, we should get the following behaviour:

: java MultiplesLoopRange 20 30 3
21 24 27 30

This time, if we specify that the start-number is larger than the stop-number, then the program should work backwards through the numbers; for example:

: java MultiplesLoopRange 30 20 3
30 27 24 21

Note

Java also has a decrement operator -- which is the counterpart of the increment operator ++. That is, some_var--; is equivalent to the statement some_var = some_var - 1.

Note

Java has a modulo operator %. For example to see if the variable num contains an even value, we evaluate the boolean expression num % 2 == 0.

An automated test has been created for this exercise: MultiplesLoopRangeTest.java.