# Exercises on control flow

## IF

Note: this is so called pseudo code. It can't be compiled. The idea is that you shall focus on studying the if statement. No compilation is needed or possible.

### Q-if-1

We have the following code:

```if (nrOfStudents > 90) {
bookBigRoom();
} else {
bookMediumRoom();
}
```

a) let’s pretend that the number of students, reflected in the variable nrOfStudents, are 123. What [statement] is reached?

Expand using link to the right to see a hint.

bookBigRoom

b) let’s pretend that the number of students, reflected in the variable nrOfStudents, are 90. What statement is reached?

Expand using link to the right to see a hint.

bookMediumRoom

c) let’s pretend that the number of students, reflected in the variable nrOfStudents, are 89. What statement is reached?

Expand using link to the right to see a hint.

bookMediumRoom

### Q-if-2

We have the following code.

```if (nrOfStudents > 90) {
bookBigRoom();
} else if (nrOfStudents > 20) {
bookMediumRoom();
} else {
bookSmallRoom();
}
```

a) let’s pretend that the number of students, reflected in the variable nrOfStudents, are 123. What statement is reached?

Expand using link to the right to see a hint.

bookBigRoom, since 123 is greater than 90

b) let’s pretend that the number of students are 90. What statement is reached?

Expand using link to the right to see a hint.

bookMediumRoom, since 90 is not greater than 90 but greater than 20

c) let’s pretend that the number of students are 89. What statement is reached?

Expand using link to the right to see a hint.

bookMediumRoom, since 89 is not greater than 90 but greater than 20

d) let’s pretend that the number of students are 21. What statement is reached?

Expand using link to the right to see a hint.

bookMediumRoom, since 21 is not greater than 90 but greater than 20

e) let’s pretend that the number of students are 20. What statement is reached?

Expand using link to the right to see a hint.

bookSmallRoom, since 20 is not greater than 90 nor greater than 20

f) let’s pretend that the number of students are 5. What statement is reached?

Expand using link to the right to see a hint.

bookSmallRoom, since 5 is not greater than 90 nor greater than 20

g) let’s pretend that the number of students are 0. What statement is reached?

Expand using link to the right to see a hint.

bookSmallRoom, since 0 is not greater than 90 nor greater than 20

### Q-if-3

We have the following code.

```if (nrOfStudents > 90) {
bookBigRoom();
} else if (nrOfStudents > 20) {
bookMediumRoom();
} else if (nrOfStudents > 0) {
bookSmallRoom();
}
```

a) let’s pretend that the number of students are 5. What statement is reached?

Expand using link to the right to see a hint.

bookSmallRoom

b) let’s pretend that the number of students are 0. What statement is reached?

Expand using link to the right to see a hint.

no statement inside the IF-statement is reached - execution will continue after the whole IF-statement

Verify yourself by using the file: Main.java

Verify yourself by using the file: Main.java

The last two Main.java files are two distinct files!

### Q-if-4

Look at the source code below:

```int age = 18;
if (age > 100) {
System.out.println("Wow, you're over hundred years old!");
} else {
System.out.println("You're still young");
}
```

a) What do you think will be printed out?

Expand using link to the right to see a hint.

You're still young

c) Change the value of age to 100. What will happen?

Expand using link to the right to see a hint.

Nothing, it will still print You're still young (since 100 is not greater than 100).

d) Change the value of age to 101. What will happen?

Expand using link to the right to see a hint.

"Wow, you're over hundred years old!" will be printed (because 101 is indeed greater than 100).

### Q-if-5

Look at the source code below:

```int age = 18;
if (age > 100) {
System.out.println("Wow, you're over hundred years old!");
} else if (age > 80) {
System.out.println("Wow, you're kind of old!");
} else {
System.out.println("You're still young");
}
```

a) What do you think will be printed out?

Expand using link to the right to see a hint.

"You're still young" because 18 is not greater than 100 and it is not greater than 80.

b) Compile the file and execute it to see what is actually printed.

Expand using link to the right to see a hint.

Did you do it? Did it work? You need to put the code in a class and inside the main method.

```public class TestMyAge {

public static void main(String[] args) {

int age = 18;
if (age > 100) {
System.out.println("Wow, you're over hundred years old!");
} else if (age > 80) {
System.out.println("Wow, you're kind of old!");
} else {
System.out.println("You're still young");
}

}
}
```

Save in a file called TestMyAge.java and compile and run with: `javac TestMyAge.java && java TestMyAge`

c) Change the value of age to 80. What will happen?

Expand using link to the right to see a hint.

Try it yourself and save, recompile and run.

d) Change the value of age to 90. What will happen?

Expand using link to the right to see a hint.

Try it yourself and save, recompile and run.

e) Change the value of age to 100. What will happen?

Expand using link to the right to see a hint.

Try it yourself and save, recompile and run.

f) Change the value of age to 101. What will happen?

Expand using link to the right to see a hint.

Try it yourself and save, recompile and run.

## WHILE

Note: this is so called pseudo code. It can't be compiled. The idea is that you shall focus on studying the while statement. No compilation is needed or possible.

### Q-W-1

Look at the code below.

```    while (true) {
setColor(RED);
}
```

How long does it execute?

Expand using link to the right to see a hint.

For ever. The while loop continues until the expression is false. Since the expression is always true the loop will continue for ever.

### Q-W-2

Look at the code below.

```    while (false) {
setColor(RED);
}
```

How many loops will be executed (if the code would compile, and later be run)?

Note: this is a trick question ;-)

Expand using link to the right to see a hint.

The code doesn't compile, actually. The compiler will see that the setColor(RED) statement is unreachable, because `false` will always be false, and the loop can't be entered!

Even if the code would have compiled, the answer would have been No loops at all. The while loop starts and continues if the test expression is true. Since the expression is always false the loop would never be entered (which is why the code doesn't compile).

### Q-W-3

Look at the code below.

```    int counter = 0;
while (counter < 3) {
setColor(RED);
counter++;
}
```

How many loops will be executed?

Expand using link to the right to see a hint.

Let's calculate this ourselves.

• We start with counter having the value 0, which is less than 3 so we're entering the loop. In that loop we increase, using ++, counter with 1.
• counter is now 1, which is still less than 3, so let's loop one more time and increase counter.
• counter is now 2, which is still less than 3, so let's loop one more time and increase counter.
• counter is now 3, which is NOT less than 3 so let's NOT loop anymore.

### Q-W-4

Look at the code below.

```    int counter = 0;
while (counter < 3) {
setColor(RED);
}
```

How many loops will be executed?

Expand using link to the right to see a hint.

For ever. The expression checks if counter is less than 3. Since counter is 0 it is also less than 3. In the loop no changes are made to the counter variable so the loop continues for ever.

### Q-W-5

Let's assume that the method getTemperature is connected to a thermometer and that the method returns an integer value which gives us the temperature in Celsius.

```while (getTemperature() < 20) {
heatUp();
}
```

Btw, heatUp() is a so called method connected to a radiator which will heat up the room. So called methods always have a name and parentheses.

Try to explain how the code works.

Expand using link to the right to see a hint.

Until the temperature is 20 degress or higher (which is to say, while temperature is lower than 20) we're calling heatUp method. When the room is 20 we continue with some other thing.... (the code below if there is more code there...).

### Q-W-6

Look at the following code in

```  System.out.println("Hi there..");
System.out.println("Hi there..");
System.out.println("Hi there..");
System.out.println("Hi there..");
```

Replace the four print statements with a while loop and one print statement.

Expand using link to the right to see a hint.

```  int i=0;
while (i<4) {
System.out.println("Hi there..");
i++;
}
```

## FOR

### Q-FOR-1

Look at the code below

```for (int i = 0; i < 3; i++) {
setColor(RED);
}
```

How many loops will be executed?

Expand using link to the right to see the full content.

Let's calculate.

• The loops starts with i=0, which is less than 3. One loop so far. After that loop, i is incremented (i++).
• Next loop i=1. Still less than 3. One more loop, so 2 loops in total. After that loop, i is incremented (i++).
• Next loop i=2. Still less than 3. One more loop, so 3 loops in total. After that loop, i is incremented (i++).
• Next loop i=3, which is NOT less than 3, so we're exiting the loop.

### Q-FOR-2

Note: this is so called pseudo code. It can't be compiled. The idea is that you shall focus on studying the for statement. No compilation is needed or possible. Look at the code below

```for (int i = 0; i < 1; i++) {
setColor(RED);
}
```

How many loops will be executed?

Expand using link to the right to see the full content.

1 loop.

### Q-FOR-3

Look at the code below

```for (int i = 0; i < 0; i++) {
setColor(RED);
}
```

How many loops will be executed?

Expand using link to the right to see the full content.

0 loops

### Q-FOR-4

Look at the code below

```for (int i=3; i>0; i--) {
setColor(RED);
}
```

How many loops will be executed?

Expand using link to the right to see the full content.

3 loops.

• First loop i=3, which is greater than 0. Decrease i. One loop so far
• Next loop i=2, which is still greater than 0. Decrease i. Two loops so far.
• Next loop i=1, which is still greater than 0. Decrease i. Three loops so far.
• Next loop i=0, which is NOT greater than 0. .... loop is not entered again.

### Q-FOR-5

Look at the code below

```int counter = 0;
while (counter < 3) {
System.out.println("Hello");
counter++;
}
```

Rewrite the code above to a for-loop.

Expand using link to the right to see the full content.

```for (int counter = 0; counter < 3; counter++) {
System.out.println("Hello");
}
```

### Q-FOR-6

Optional - rewrite all the loops from `for` to `while` and vice versa. We won't give you a solution but you can write the code, compile and run, to see if you succeeded.

Expand using link to the right to see the full content.

Try it out. You will never learn Java without practice!