Free Ebook cover Learn to program in complete Java, from programming logic to advanced

Learn to program in complete Java, from programming logic to advanced

3.5

(4)

238 pages

Flow control (if, else, switch, loops): else conditional structure

Capítulo 41

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

The else conditional structure in Java is a fundamental part of flow control in any program. When we are writing code, we often need to perform different actions depending on certain conditions. The if statement allows us to check a condition and execute a block of code if that condition is true. However, what if we want to do something different when the condition is not met? This is where the else conditional structure comes in.

In its most basic form, the else structure is used in conjunction with if as follows:

if (condition) {
    // block of code to be executed if the condition is true
} else {
    // block of code to be executed if the condition is false
}

This structure tells the program to execute the first block of code if the condition specified by if is true. If it is not, the program jumps to the code block inside else and executes it. It's important to note that the else block is completely optional; you can have a if without a else if you just want to execute code for the true condition.

Sometimes we want to check several conditions in sequence. To do this, we can use the else if structure, which allows you to queue up multiple conditional tests:

if (firstCondition) {
    // Execute if the first condition is true
} else if (secondCondition) {
    // Execute if the second condition is true
} else {
    // Execute if none of the above conditions are true
}

With this structure, if the first condition is not true, the program checks the second condition, and so on. If none of the if or else if conditions are true, the code block inside else will be executed.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

In addition, it is possible to nest conditional structures inside others, which means you can have a if or else if inside another if code> or else. However, it is important to be careful about excessive nesting as this can make code difficult to read and maintain.

A practical example of how the conditional structure else is used can be seen in the following code snippet:

int score = 75;

if (score >= 90) {
    System.out.println("Excellent!");
} else if (score >= 70) {
    System.out.println("Good!");
} else if (score >= 50) {
    System.out.println("Satisfactory");
} else {
    System.out.println("Insufficient");
}

In this example, a student's score is evaluated and a different message is printed depending on the score range the student is in. If the score is 90 or more, "Excellent!" is printed. If it's between 70 and 89, "Good!" is printed, and so on. If the score is less than 50, "Insufficient" is printed.

It is important to mention that the conditional structure else can only be used immediately after a if or else if, and cannot exist by itself only. Additionally, there can only be one else for each if, but there can be multiple else if if necessary.

In summary, the else conditional structure is a powerful tool for controlling the flow of a Java program. It allows developers to handle multiple conditions and make decisions based on program variables and states. By using if, else if, and else effectively, you can create complex, responsive programs that react differently to different inputs and situations .

Now answer the exercise about the content:

_Which statement about the `else` conditional structure in Java is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

The else block is executed when the condition specified in if is false. It cannot be used alone without a preceding if or else if, and there may only be one else block associated with each if.

Next chapter

Flow control (if, else, switch, loops): else if conditional structure

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.