Skip to main content
eLearner.app
Module 1 · Lesson 2 of 22/14 in the course~12 min
Module lessons (2/2)

Flow Control

To make our programs dynamic and capable of making decisions, we use control flow structures: conditional statements and loops.

Conditionals: if, else if, and else

The if statement executes a block of code only if a specific boolean condition is true.

Code
int temperature = 25;

if (temperature > 30) {
    System.out.println("Fa molto caldo!");
} else if (temperature >= 20) {
    System.out.println("Il clima è piacevole.");
} else {
    System.out.println("Fa freddo.");
}

Logical Operators

We can combine multiple conditions using logical operators:

  • && (AND): Returns true if both conditions are true.
  • || (OR): Returns true if at least one of the conditions is true.
  • ! (NOT): Reverses the logical value of the condition.
Code
boolean hasLicense = true;
boolean hasCar = false;

if (hasLicense && hasCar) {
    System.out.println("Puoi guidare.");
}

Loops: while and for

Loops allow us to repeatedly execute a block of code.

The while Loop

Executes the code as long as the specified condition remains true.

Code
int count = 1;
while (count <= 3) {
    System.out.println("Conteggio: " + count);
    count++; // Increment count by 1
}

The for Loop

Ideal when we know in advance the number of iterations to perform. The syntax requires initialization, a termination condition, and an increment expression.

Code
for (int i = 0; i < 5; i++) {
    System.out.println("Iteration: " + i);
}

Advanced Conditionals: the switch

When we need to compare a single variable against multiple constant values, the switch statement makes the code much cleaner than multiple chained if-else blocks.

Code
int dayOfWeek = 3;
switch (dayOfWeek) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Other day");
}

Loop Control: break and continue

We can alter the normal flow of a loop using two special statements:

  • break: immediately terminates execution of the innermost loop, exiting it completely.
  • continue: skips the rest of the code in the current iteration and jumps directly to evaluating the condition for the next iteration (or the increment expression in a for loop).
Code
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue; // Skips printing for the number 3
    }
    System.out.println(i);
}

Try it yourself

Exercise#java.m1.l2.e1
Attempts: 0Loading…

Complete the code by checking the score variable: if it is greater than or equal to 60, print Pass, otherwise print Fail.

Loading editor…
Show hint

Use `if (score >= 60) { ... } else { ... }` and print the exact string requested.

Solution available after 3 attempts

Exercise#java.m1.l2.e2
Attempts: 0Loading…

Write a for loop to print the numbers from 1 to 5 (inclusive) on separate lines.

Loading editor…
Show hint

Use initialization `int i = 1`, condition `i <= 5` and increment `i++` inside the header of the `for` loop.

Solution available after 3 attempts

Exercise#java.m1.l2.e3
Attempts: 0Loading…

Complete the code using a while loop to print the numbers from 3 down to 1 (inclusive) on separate lines.

Loading editor…
Show hint

Write `while (count >= 1) { ... }` printing `count` and then decrementing it using `count--;`.

Solution available after 3 attempts