Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Sentencia If-Else | Tipos Básicos, Operaciones
Principios Básicos de Java
course content

Contenido del Curso

Principios Básicos de Java

Principios Básicos de Java

1. Iniciando
2. Tipos Básicos, Operaciones
3. Loops
4. Arrays
5. String

book
Sentencia If-Else

Comparison Operators

To start, let's understand what comparison operators are.

The following are comparison operators: >, <, >=, <=, ==, !=.

Let's quickly go through what each of them does:

  • > (greater than) compares if the value on the left is greater than the value on the right;
  • < (less than) compares if the value on the left is less than the value on the right;
  • >= (greater than or equal to) compares if the value on the left is greater than or equal to the value on the right;
  • <= (less than or equal to) compares if the value on the left is less than or equal to the value on the right;
  • == (equal to) compares if the values on both sides are equal;
  • != (not equal to) compares if the values on both sides are not equal.

These operators are used to compare values and return a boolean result (true or false) based on the comparison.

If Statement

The if statement is an essential part of any program. With the if statement, you can set conditions for your program. The syntax and the diagram of the if statement look like this:

If Statement

The if statement is an essential part of any program. With the if statement, you can set conditions for your program. The syntax and the diagram of the if statement look like this:

java

Main

copy
12345
if (condition) { // code to be executed if it passes the condition } else { // code to be executed if it don't pass the condition }

Below is a flowchart showing the use of an if statement. Before entering the if block, we check the condition. If the condition evaluates to true, we enter the if block and perform the necessary operations. If the condition evaluates to false, we skip the if block and continue with the code.

Let's look at an example with real values:

java

Main

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { //you can change the values of variables a and b to test the if statement double a = 13.71; double b = 14.01; if (b > a) { System.out. println("b is greater than a"); } if (a > b) { System.out. println("a is greater than b"); } } }

But it doesn't look elegant when we have two separate if statements. We have a dedicated syntax for situations like this called the if-else statement.

Let's see how we can improve the code above using the if-else statement:

But it doesn't look elegant when we have two separate if statements. We have a dedicated syntax for situations like this called the if-else statement.

Let's see how we can improve the code above using the if-else statement:

java

Main

copy
1234567891011121314
package com.example; public class Main { public static void main(String[] args) { //you can change the values of variables a and b to test the if statement double a = 13.71; double b = 14.01; if (b > a) { System.out.println("b is greater than a"); } else { System.out.println("a is greater than or equal to b"); } } }

Here is the block scheme of If-Else Statement:

Here is the block scheme of If-Else Statement:

Let's examine a code fragment where we compare the values of two variables for equality:

java

Main

copy
1234567891011121314
package com.example; public class Main { public static void main(String[] args) { // You can change the values of variables `a` and `b' to test the `if` statements int a = 10; int b = 10; if (a == b) { System.out.println("a equals b"); } else { System.out.println("a is not equal to b"); } } }

else-if chain

It is worth mentioning another statement called the else-if statement.

When we need to specify multiple different execution conditions, we can use the following syntax:

else-if chain

It is worth mentioning another statement called the else-if statement.

When we need to specify multiple different execution conditions, we can use the following syntax:

java

Main

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { // You can change the values of variables `a` and test the `if-else` statement int a = 25; int b = 13; if (a > b) { System.out.println("a is greater than b"); } else if (a == b) { System.out.println("a equals b"); } else { System.out.println("b is greater than a"); } } }

In the code above, we can see that multiple different conditions are used. Thus, it follows a simple algorithmic chain. If the first condition is false, check the second one, and so on. We continue doing this until we get true, or if all conditions return false, we enter the familiar else block.

1. What will be printed to console after code execution?

2. What will be printed to console after code execution?

What will be printed to console after code execution?

What will be printed to console after code execution?

Selecciona la respuesta correcta

What will be printed to console after code execution?

What will be printed to console after code execution?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt