Object-Oriented Programming with Java, part I + II

cc

This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.

Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
Adapted for Informatica by: Ruud Hermans

The course is maintained by Technische Informatica Breda


Changing the flow of execution in a loop

Break

It is also possible to change the flow of execution in a loop. This is done with the break and continue keywords.

The break keyword immediately breaks the loop, and jumps out of the loop. This is usually used as little as possible, as the code execution can become very chaotic with a lot of break commands. An example of using this is rewriting the code from section 11.3, which can also be written as

System.out.println("Type your age ");
int age;
while (true) {
    age = Integer.parseInt(reader.nextLine());

    if (age >= 5 && age <= 85) {  // age between 5 AND 85
        break;  // end the loop
    }

    System.out.println("You are lying!");
    if (age < 5) {
        System.out.println("You are so young that you cannot know how to write!");
    } else {  // that means age is over 85
        System.out.println("You are so old that you cannot know how to use a computer!");
    }

    System.out.println("Type your age again: ");
}

System.out.println("Your age is " + age);

Note that this example uses a while(true) command, which is another bad practice

Continue

The continue statement jumps back to the beginning of the loop. This can be used to ‘skip’ certain values of variables. An example

int i = 0;
while (i < 100) {
    i++;
    if(i % 2 == 0) {
        continue;
    }
    if(i % 3 == 0) {
        continue;
    }
    if(i % 5 == 0) {
        continue;
    }
    System.out.println(i);
}

This example will output all values that are not dividable by 2, 3, 5. Note that the output does not include 0, as this is skipped by the i++ command.

Exercise break-continue-1: Loops, ending and remembering

This set of exercises will form one larger program when put together. We create the program by adding features exercise by exercise. If you do not finish all the exercises you can still send them to be reviewed by the exercise robot. To do that, click the “submit” button, which has a picture of an arrow and is located on the right of the testing button. Even though the exercise robot complains about tests in the incomplete exercises, you will still get points for the parts you have completed.

Note: from now on every sub-exercise of a larger exercise (like 36.1) has the same value as an exercise without sub-exercises. It means that exercise 36 as a whole corresponds to five normal exercises.

Exercise break-continue-1.1: Reading numbers

Create a program that asks the user to input numbers (integers). The program prints “Type numbers” until the user types the number -1. When the user types the number -1, the program prints “Thank you and see you later!” and ends.

Type numbers:
~~5~~
~~2~~
~~4~~
~~-1~~
Thank you and see you later!

Exercise break-continue-1.2: The sum of the numbers

Develop your number reading program by adding the following feature: the program should print the sum of the numbers entered by the user (without the number -1).

Type numbers:
~~5~~
~~2~~
~~4~~
~~-1~~
Thank you and see you later!
The sum is 11

Exercise break-continue-1.3: Summing and counting the numbers

Develop your number reading and summing program by adding the following feature: the program should print how many numbers the user typed (without the number -1).

Type numbers:
~~5~~
~~2~~
~~4~~
~~-1~~
Thank you and see you later!
The sum is 11
How many numbers: 3

Exercise break-continue-1.4: Counting the average

Develop your number reading, summing and counting program by adding the following feature: the program should print the average of the numbers the user typed (without the number -1).

Type numbers:
~~5~~
~~2~~
~~4~~
~~-1~~
Thank you and see you later!
The sum is 11
How many numbers: 3
Average: 3.666666666666

Exercise break-continue-1.5: Even and odd numbers

Develop your program by adding the following feature: the program should print the number of even and odd numbers that the user typed (without the number -1).

Type numbers:
~~5~~
~~2~~
~~4~~
~~-1~~
Thank you and see you later!
The sum is 11
How many numbers: 3
Average: 3.666666666666
Even numbers: 2
Odd numbers: 1

Note: creating a program in small steps

In these exercises we actually created one single program, but programming happened in very small steps. This is ALWAYS the preferred way to program.

When you are programming something, no matter if it is an exercise or a project of your own, it is advised to do it in very tiny pieces. Do not ever try to solve the whole problem in one go. Start with something easy, something you know that you can do. In this recent set of exercises, for example, we focused first on stopping the program when the user types -1. When one part of the program is complete and working, we can move on to work out the solution for the next sub-problem of the big main problem.

Some of the exercises in this course are sliced into smaller pieces like the set of exercises we just introduced. Usually the pieces need to be sliced again into smaller pieces depending on the problem. It is advised that you execute the whole program after almost every new line of code you write. This enables you to be sure that your solution is going in the right and working direction.