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): If conditional structure

Capítulo 40

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

The conditional structure if is one of the fundamental concepts in programming, not only in Java, but in almost all programming languages. It allows a program to execute certain instructions based on the evaluation of a Boolean condition, that is, an expression that can be true (true) or false (false).< /p>

In its simplest form, a if statement in Java has the following syntax:

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

If the condition evaluated inside the parentheses is true, the associated block of code, delimited by the braces, will be executed. Otherwise, the program will continue execution to the next statement after the if block.

A common extension of the if statement is the use of else, which allows you to define a block of code to be executed if the if is not true. The syntax is 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
}

In addition, it is possible to chain multiple conditions using the else if clause, thus creating a chain of conditional checks:

if (condition1) {
    // block of code to be executed if condition1 is true
} else if (condition2) {
    // block of code to be executed if condition2 is true
} else {
    // block of code to be executed if none of the previous conditions are true
}

Another important conditional structure in Java is the switch. Unlike if, which can evaluate a wide range of true or false conditions, switch is used to compare a single variable with a series of constant values, known as cases (case). The basic syntax of switch is as follows:

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

switch (variable) {
    case value1:
        // code block to be executed if variable == value1
        break;
    case value2:
        // code block to be executed if variable == value2
        break;
    // ...
    default:
        // block of code to be executed if none of the previous case matches are true
}

It is important to note the use of the break keyword at the end of each case. break is used to end execution within the switch block and prevent the program from continuing to test subsequent cases. The default clause is optional and serves as a kind of "else" for the switch, being executed when none of the cases match the value of the variable.

As for loops, Java provides several structures to perform repetitions, that is, to execute a block of code several times. The most common loops are for, while and do-while. The for loop is generally used when the number of iterations is known in advance:

for (initialization; condition; increment) {
    // block of code to be repeated
}

The while loop, on the other hand, executes a block of code as long as a condition is true and is more suitable when the number of iterations is not known in advance:

while (condition) {
    // block of code to be repeated
}

Finally, the do-while loop is similar to while, but ensures that the code block is executed at least once, as the condition is evaluated after block execution:

do {
    // block of code to be repeated
} while (condition);

Understanding these control flow structures is essential for any programmer, as they are the basis for creating programs that can make decisions and perform repetitive tasks. In Java, mastery of these structures allows the developer to create complex and efficient programs capable of dealing with a variety of scenarios and conditions.

Now answer the exercise about the content:

_Which of the following statements is true regarding the use of the `if` conditional structure in Java?

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

You missed! Try again.

The if statement in Java is used to execute a block of code if the evaluated Boolean condition is true. This is a fundamental aspect of using if conditional structures in Java, allowing a program to make decisions based on conditions.

Next chapter

Flow control (if, else, switch, loops): else 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.