lettura simple

Flow Control Statements in Java

In Java, flow control statements are fundamental to a program's logic. They enable you to guide the execution of code based on specific conditions, repeat actions, and make decisions.

Conditional Statements

if else

The `if` statement allows you to execute a block of code only when a certain condition is met, while the `else` statement provides an alternative for when that condition is not satisfied.

if (temperature > 30) {
    turnOnAirConditioning();
} else {
    turnOffAirConditioning();
}

In this example, the air conditioning is turned on if the temperature exceeds 30 degrees; otherwise, it’s turned off.

else if

When you need to evaluate multiple conditions within an if-else structure, the `else if` clause allows you to create a sequence of decisions.

if (day == "Monday") {
    print("The week is starting.");
} else if (day == "Friday") {
    print("The week is almost over.");
} else {
    print("It's a regular day.");
}

In this case, the program prints a different message depending on the day of the week.

switch

When you have a variable that can take one of several possible values, `switch` offers an efficient way to handle them.

switch (trafficSignal) {
    case "Red":
        stopCar();
        break;
    case "Yellow":
        prepareToStop();
        break;
    case "Green":
        goCar();
        break;
    default:
        signalError();
}

Depending on the traffic signal, the car will either stop, prepare to stop, or proceed.

Loops

Java provides three primary loop constructs: for, while, and do-while.

for

The `for` loop is ideal for running an operation a specific number of times.

for (int i = 0; i < 10; i++) {
    print("Count: " + i);
}

In this example, the loop prints the numbers from 0 to 9.

while

The `while` loop continues to execute a block of code as long as a given condition remains true.

while (notFinished()) {
    continueProcessing();
}

The loop keeps processing until the task is complete.

do-while

Similar to `while`, the `do-while` loop ensures that the code block is executed at least once, regardless of the condition.

do {
    requestInput();
} while (inputIsInvalid());

In this example, the user is prompted for input at least once, and the loop continues until the input is valid.

Jump Statements

break

The `break` statement is used to exit immediately from a loop or a `switch` statement.

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exits the loop when i is 5
    }
    print("Count: " + i);
}

continue

The `continue` statement skips the current iteration and moves on to the next one.

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // Skips even numbers
    }
    print("Odd number: " + i);
}

return

The `return` statement ends the execution of a method and, if applicable, returns a value to the caller.

public int sum(int a, int b) {
    return a + b; // Returns the sum of a and b
}

These flow control statements are essential for constructing complex and adaptable logic in Java.

They empower you to create programs that dynamically respond to conditions and data encountered during execution.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin