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): Comparison operators

Capítulo 43

Estimated reading time: 6 minutes

Audio Icon

Listen in audio

0:00 / 0:00

7.4. Flow Control in Java: Comparison Operators and Control Structures

Flow control is a fundamental aspect of Java programming, allowing a program to make decisions and perform repetitions based on conditions. Using comparison operators and control structures like if, else, switch, and loops like for, while, and do-while, programmers can create dynamic and interactive programs. In this section, we will explore these concepts in detail.

Comparison Operators

Comparison operators are used to compare two values ​​and return a Boolean value (true or false) as a result. Comparison operators in Java are:

  • == (equal to)
  • != (different from)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)
int a = 10;
int b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // false
System.out.println(a < b); // true
System.out.println(a >= 10); // true
System.out.println(a <= 9); // false

Control Structure if and else

The if control structure allows a block of code to be executed only if a specified condition is true. If the condition is false, the program can choose to do nothing or execute an alternative block of code using else.

int score = 75;
if (score >= 90) {
  System.out.println("Excellent!");
} else if (score >= 70) {
  System.out.println("Good!");
} else {
  System.out.println("Try again!");
}

The example above checks the punctuation and prints a corresponding message. If the score is greater than or equal to 90, it prints "Excellent!". If it is between 70 and 89, it prints "Good!". Otherwise, it prints "Try again!".

Control Structure switch

The switch structure is an alternative to using multiple if and else if. It allows program flow to be directed to different blocks of code based on the value of a variable.

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

char grade = 'B';
switch (grid) {
  case 'A':
    System.out.println("Excellent!");
    break;
  case 'B':
    System.out.println("Very good!");
    break;
  case 'C':
    System.out.println("Good!");
    break;
  case 'D':
    System.out.println("Satisfactory!");
    break;
  case 'F':
    System.out.println("Failed!");
    break;
  default:
    System.out.println("Invalid note");
    break;
}

In the example above, the program prints "Very good!" because the value of grade is 'B'. The break keyword is used to exit the switch after the corresponding case has been executed.

Loops

Loops are structures that repeat a block of code as long as a condition is true. The three main types of loops in Java are for, while, and do-while.

Loop for

The for loop is often used when the number of iterations is known. It consists of three parts: initialization, condition, and increment/decrement.

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

The code above prints the numbers from 0 to 4. The variable i is initialized to 0, and the loop continues as long as i is less than 5, incrementing i by 1 at each iteration.

Loop while

The while loop executes a block of code repeatedly as long as a specified condition is true. The condition is evaluated before each iteration.

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

The example above has the same behavior as the previous for loop, printing the numbers 0 to 4.

Loop do-while

The do-while loop is similar to the while, but the condition is evaluated after the code block has been executed, ensuring that the block is executed at least once .

int count = 0;
of {
  System.out.println("count = " + count);
  count++;
} while (count < 5);

Just like the previous examples, this code prints the numbers 0 to 4.

Conclusion

Comparison operators and control structures are essential for creating dynamic programs in Java. They allow developers to write code that can make decisions and perform repetitive tasks. The practice and understanding of theseThese concepts are fundamental for anyone who wants to learn Java in depth.

Once you are comfortable with these control structures, you can create more complex and functional programs. Remember that control flow is the backbone of programming logic, and delving into these areas will open doors to advanced and efficient software development.

Now answer the exercise about the content:

_Which of the following comparison operators in Java is used to check whether two values ​​are equal?

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

You missed! Try again.

  • ==: Used to check if two values are equal in Java. It returns a Boolean value true if the values are equal, otherwise false.

Next chapter

Flow control (if, else, switch, loops): Logical operators

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