7.5. Flow Control in Java: Logical Operators and Decision Structures

Flow control in Java is one of the fundamental pillars for developing effective programs. Through the intelligent use of decision structures such as if, else, switch and repetition loops such as for, < code>while and do-while, it is possible to create complex and robust logic. In this chapter, we will explore the use of logical operators and control flow structures, which are essential for any Java developer who wants to progress from basic to advanced.

Logical Operators

Logical operators are used to form complex Boolean expressions, which result in a true or false value. The main logical operators in Java are:

  • AND (&&): Returns true if both expressions are true.
  • OR (||): Returns true if at least one of the expressions is true.
  • NOT (!): Inverts the value of the expression, from true to false and vice versa.

By combining these operators, we can build more complex conditions to control the flow of our program.

Decision Structure if

The if structure is the most basic form of flow control. It allows the program to execute a block of code only if a specific condition is true. For example:


if (condition) {
    // Block of code that will be executed if the condition is true
}

Structure else

else is used in conjunction with if to execute an alternative code block when the if condition is not satisfied.

p>

if (condition) {
    // Execute if the condition is true
} else {
    // Execute if condition is false
}

else if structure

To test multiple conditions, we can chain multiple if structures with else if:


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 previous conditions are true
}

switch

structure

switch is an alternative to if that can make code more readable when we have many conditions based on the same value. Each case is defined by the case keyword, followed by a value and a break to prevent subsequent cases from executing.


switch (variable) {
    case value1:
        // Execute if variable == value1
        break;
    case value2:
        // Execute if variable == value2
        break;
    default:
        // Execute if none of the above cases are true
}

Repeating Loops

Repeat loops are used to execute a block of code multiple times. Java provides several loop structures:

  • for: Executes a block of code a specified number of times.
  • while: Executes a block of code while a condition is true.
  • do-while: Similar to while, but ensures that the code block is executed at least once.

Loop for


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

while

loop

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

do-while loop


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

Practical Examples

Let's look at some examples of how these structures can be used in a Java program:


// if-else example
int grade = 75;
if (grade >= 70) {
    System.out.println("Approved");
} else {
    System.out.println("Failed");
}

// Switch example
char grade = 'B';
switch (grid) {
    case 'A':
        System.out.println("Excellent");
        break;
    case 'B':
    case 'C':
        System.out.println("Well done");
        break;
    case 'D':
        System.out.println("You passed");
        break;
    case 'F':
        System.out.println("Better try again");
        break;
    default:
        System.out.println("Invalid note");
}

// for example
for (int i = 0; i < 5; i++) {
    System.out.println("i = " + i);
}

// while example
int i = 0;
while (i < 5) {
    System.out.println("i = " + i);
    i++;
}

// do-while example
int j = 0;
of {
    System.out.println("j = " + j);
    j++;
} while (j < 5);

Understanding and correctly applying logical operators and flow control structures is essential for any Java developer. With practice, these tools become intuitive and allow the construction of complex and efficient programs.

Now answer the exercise about the content:

_Which of the following logical operators in Java is used to return `true` only if both expressions are true?

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

You missed! Try again.

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

Next page of the Free Ebook:

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

6 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text