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.
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): Returnstrueif both conditions are true.||(OR): Returnstrueif at least one of the conditions is true.!(NOT): Reverses the logical value of the condition.
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.
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.
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.
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 aforloop).
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skips printing for the number 3
}
System.out.println(i);
}
Try it yourself
Complete the code by checking the score variable: if it is greater than or equal to 60, print Pass, otherwise print Fail.
Show hint
Use `if (score >= 60) { ... } else { ... }` and print the exact string requested.
Solution available after 3 attempts
Write a for loop to print the numbers from 1 to 5 (inclusive) on separate lines.
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
Complete the code using a while loop to print the numbers from 3 down to 1 (inclusive) on separate lines.
Show hint
Write `while (count >= 1) { ... }` printing `count` and then decrementing it using `count--;`.
Solution available after 3 attempts