Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If-Else Statement | Basic Types, Operations
course content

Course Content

Java Basics

If-Else StatementIf-Else Statement

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:

java

Main.java

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.

Note

By the way, the fact that all the code is organized and executed in blocks is called structuring. It's precisely because Java is a strictly structured programming language that it is quite popular.

Let's look at an example with real values:

java

Main.java

In this code, we establish a condition. If the value of a is greater than that of b, we display information about it. If the value of b exceeds a, we display different information about it.

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.java

We can see how we improved our previous code using the if-else statement. In simple terms, we check if the value of variable b is greater, and if the returned value is false, we enter the else block, where we display a different message.

Here is the block scheme of If-Else Statement:

Note

It's also worth noting that a boolean value should be stored inside the parentheses after the if-else statement. We can write it as boolean bool = b > a; In this case, the variable bool will hold the value true.

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

java

Main.java

Here, we are checking if the values of a and b are equal and displaying information about it. Since both a and b have a value of 10, the result will be true, so we display the corresponding message.

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.java

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 is the result of this code?
2. What will be printed to console after code execution?

What is the result of this code?

Select the correct answer

What will be printed to console after code execution?

Select the correct answer

Everything was clear?

Section 2. Chapter 5
course content

Course Content

Java Basics

If-Else StatementIf-Else Statement

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:

java

Main.java

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.

Note

By the way, the fact that all the code is organized and executed in blocks is called structuring. It's precisely because Java is a strictly structured programming language that it is quite popular.

Let's look at an example with real values:

java

Main.java

In this code, we establish a condition. If the value of a is greater than that of b, we display information about it. If the value of b exceeds a, we display different information about it.

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.java

We can see how we improved our previous code using the if-else statement. In simple terms, we check if the value of variable b is greater, and if the returned value is false, we enter the else block, where we display a different message.

Here is the block scheme of If-Else Statement:

Note

It's also worth noting that a boolean value should be stored inside the parentheses after the if-else statement. We can write it as boolean bool = b > a; In this case, the variable bool will hold the value true.

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

java

Main.java

Here, we are checking if the values of a and b are equal and displaying information about it. Since both a and b have a value of 10, the result will be true, so we display the corresponding message.

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.java

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 is the result of this code?
2. What will be printed to console after code execution?

What is the result of this code?

Select the correct answer

What will be printed to console after code execution?

Select the correct answer

Everything was clear?

Section 2. Chapter 5
some-alt