Select Page

if statement in Java

An if statement in Java is a one way conditional statement that decides the execution path based on whether the condition is true or false.

nanadwumor

May 27, 2023
if statement in Java

An if statement in Java is a one way conditional statement that decides the execution path based on whether the condition is true or false.

It’s a basic form of decision making in a program.


Join other Subscribers on our YouTube channel and enjoy daily pogramming tutorials.


Syntax of the if statement

A boolean is a data type in java. It has two data types.  It’s either true or false.

Thus, a Boolean expression is an expression  that resolves to either true or false.

If the boolean expression is true, the statements under the if statement are executed.

Relational Operators

Relational operators are usually used to form the boolean expression.

The relational operators include:

>

<

>=   NOTE: > precedes =. No space between them

<=  NOTE: == is equal sign. = is assignment operator

== equal to

! = not equal to

Recommended



Examples of boolean expressions using relational operators

(1) The greater than operator. 

      Example : (10 > 5) is true

Let’s put it into an if statement.

(2) Less than (<).

Example : (4 < 9) is true

                  (42 < 9) is false

(3) Equal to (==)

In math, equal to sign is written as = but in Programming, particularly Java, we use double equal to signs. That’s ==. The = is called assignment sign in Java.

Example : x=5 means 5 is assigned to x or 5 is stored in x.

However, x==5 is read as x equals 5.

Example of boolean expressions using == include: 

(3==3) resolves to true

(3==33) resolves to false

Let’s consider an if statement using the == relational operator :

(4) Not equal to (!=)

Examples of boolean expressions involving the != sign are:

(4 != 6) resolves to true

(7 != 7) resolves to false

(5) Greater than or equal to ( >=)

Examples of boolean expressions involving the (>=) sign are:

(9 >= 4) resolves to true

(9 >= 45) resolves to false

Let’s consider an if statement involving the ( >= ) sign :

(6) Less than or equal to (<=)

Examples of boolean expressions involving the (<=)sign are: 

(9 <= 4) resolves to false

(9 <= 45) resolves to true

Let’s consider an if statement involving the ( <= ) sign :

Program that demonstrates the use of variable in an if statement.

The boolean expression is sufficient

Many beginners wonder why the if statement is not something like this:

if (booleanExpression is true) or more appropriately, 

if (booleanExpression == true)

But we just write something like this:

As a matter of fact, let’s state emphatically that the value of a boolean variable is sufficient, by itself, to control the if statement.

Example, consider this if condition :

If you consider the conditional statement, 40 > 10, it’s true that 40 is greater than 10. This means, 40 >10 resolves to true

Thus, the above statement resolves to :

if we were to write :

then that would resolve to:

This makes the statement,

superfluous or an over-kill.

Command flow of Java programs

All Java programs execute from head to bottom in a sequential manner. Example:

Output

command flow of Java program

What if we want some block of code to execute only if a certain condition is present?

In that program above, all salary workers qualified for a loan. They could all take loans. 

What if we decide in the future that only workers with a minimum amount of salary could take the loan? This can be done by using an *if statement*. Let’s rewrite the above program.

Ouput

if statement in java

In the program above, the variable salary is assigned to the value 600 which  greater than 500. Hence, the if statement is true

This means, statements in the if statement are executed.

What if the boolean expression of the if statement is false?

Let’s consider a condition where the if statment is false.

In the program below, the variable salary is assigned to the value 400 which NOT greater than 500. Hence, the if statement is false

This means, statements in the if statment are not executed.

Output

if statement in java

The if statement executes code only if its boolean expression is true

Variables can be used in the boolean expression

Ouput

if statement in java

When the if statement has more than one statement under it.

When the if statement has more than one statement under it, you have to introduce opening and closing braces around the statements.

When the statements of the if statement are not put in braces, only the first statement is considered connected to the if statement.

 if statement with more than one statement must have curly braces

When the if statement does not have curly braces, only the first statement is executed.

The program below illustrates scenario where the boolean expression is true, the  if statement has no curly brace.

Ouput

if statement in java without braces

This program below illustrates a scenario where the boolean expression is false, the  if statement has no curly brace.

Ouput

if statement in java without braces

Don’t put semi-colon at end of boolean expression of  ‘ if ‘ statement.

Ouput

if statement in java with semicolon after boolean expression

This statement shouldn’t have executed because (2>10) is false. However, because there’s semicolon at end of the if boolean condition, the if statement is disconnected from the if condition.

This implies the statement executed independently outside the scope of the if statement. That is, whether the boolean expression is true or false.

Using many ‘if ‘ statements in program to print a grade

You can use as many if statements as you want. The following program will look at how we can use many if statements to print grade of a student.

Output

Grade is B

All if statements will be tested until the suitable if statement is reached and executed. This could be an inefficient way of printing the student grades.

Using ‘if’ statement in a ‘for’ loop

Output

if statement in a for loop

In the above code, the for loop iterates 10 times and at each iteration, the if statement checks whether the value of i is an even number. If it is an even number,the boolean expression evaluates to true and the statements under it are executed.

The statement (i % 2) == 0 means that the value of i is divided by 2 and its remainder is checked if it is equal to zero. If remainder is zero, then the number is an even number. Note that an even number is a number that 2 divides without a remainder.

Putting a semi-colon before the ‘if’ statement

 In the above program, we have placed a semi-colon at the end of the if statement. This makes the if statement is null detaching the statement under the if statement from it. Thus, we see that the output spans numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. This is because the if statement no more screens for only even numbers unlike the previous program.

Negating boolean expressions of ‘if ‘ Statements

If you negate the boolean expression of an if statement, the results turn from true to false or from false to true.

That’s;

Output

Negating Boolean Expression

Let’s look at a typical Example:

Now, let’s negate the boolean expression and see its results.


Share this article


You May Also Like…

double data type in Java

double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal...

The print and println methods in Java

The print and println methods in Java

The print() and println() methods are Java's predefined methods used to display text output on the console. These...

float data type in Java

float data type in Java

It is a floating point number. It has single precision data type. It stores a floating point number of 7 digits of...