Using If statements in Java

Read Time:6 Minute, 16 Second

Table of Contents

Decision Making in Java is the source of creating decision-driven statements and executing a specific set of code in response to specified situations. The most straightforward basic decision-making statement in Java is the if statement. It is used to determine if a statement or a block of statements will be executed or not, i.e., if a condition is true, a block of statements will be executed, otherwise not.

If statement syntax:

// Statements to execute if
// condition is true

The if statement in action

  1. The if block is where control resides.
  2. Condition is the next step in the flow.
  3. The situation is examined.
    3.1 Go to Step 4 if the condition returns true.
    3.2 Go to Step 5 if the condition returns false.
  4. The body of the if-block or the if-block itself is performed.
  5. The if block is exited

After the if-statement has been evaluated, the condition will be either true or false. The if statement in Java takes boolean values and executes the block of statements underneath it if the value is true. If the curly brackets “{” and “}” are not provided after if(condition), the if statement will regard the immediate one statement to be inside its block by default. As an example,

if(condition) statementOne;

In this case, if the condition is true, the if block only considers the statementOne to be inside its block.


Using If statements in Java

Example 1: illustrating the If statement

class IfCondition { public static void main(String args[]) { int i = 50; if (i < 80) System.out.println("50 is less than 80"); // The statement is executed as though there is one statement by default System.out.println("Condition outside the if-block"); }

Example 2: illustrating the If statement

class IfCondition { public static void main(String args[]) { String str = "Codeunderscored"; int i = 2; // if block if (i == 2) { i++; System.out.println(str); } // this check is executed by default System.out.println("i = " + i); }

Example 3: Using the the If condition

class IfCondition { public static void main(String[] args) { // create a string variable String myName = "Java"; // if statement if ( myName == "Java") { System.out.println("You are now ready to Program in the Java Programming Language"); } }

Statement “else”

If the condition is false, use the else statement to specify a block of code to run.

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
int age = 32; if (age < 16) {
System.out.println("Sorry, You are under age.");
} else {
System.out.println("Welcome, you are a grown-up.");

The program would print “Sorry, you are under age.” if the age were less than 16. Because age 32 is more than 16, the condition is false in the example above. As a result, we continue to the next condition and print “Welcome, you are a grown-up” on the screen.


If…else Statement in Java

We will use examples to assist you in learning about control flow statements in Java utilizing if and if…else statements. The if..else statement is used in programming to choose amongst multiple options while running a code block. As an example, assigning awards to employees depending on the employee’s performance rating in percentage.

  • If the percentage is greater than 80%, give the employee $1000.
  • If the percentage is greater than 50, give the employee $250.
  • If the percentage is below 50, give the employee $50.

If the test phrase is true, then if statement executes a specific section of code. When the test expression is evaluated to false, however, nothing happens. We can utilize an optional else block in this situation. If the test expression is false, the statements inside the body of the else block are performed. In Java, this is known as the if…else statement.

The if…else sentence has the following syntax:

if (firstCondition) {
// blocks of code }
else if(secondCondition) {
// blocks of code }
else if (thirdCondition) {
// blocks of code }
else {
// blocks of code }

If statements are executed in this order, from top to bottom. If the test condition is true, the code in the if block’s body is executed. Furthermore, program control exits the if…else…if loop. If all of the test expressions are false, the programs’ code in the else body is run.



class Main { public static void main(String[] args) { int age = 0; // checks if number is greater than 16 if (age > 16) { System.out.println("you are an adult."); } // checks if number is less than 0 else if (number < 16) { System.out.println("You are a child."); } // if both condition is false else { System.out.println("You don't belong to any category."); } }

Based on the provided age, we’re verifying whether the person is a child, adult, or none in the example above. There are two condition phrases in this case:

  • age > 16 – determines whether a number is greater than 16 or not.
  • age < 16 – determines if the number is less than 16.

The value of age is 0 in this case. As a result, both criteria are false. As a result, the statement inside else’s body is executed.

Java nested if..else condition

It is also possible to utilize if..else statements within an if…else statement in Java. The last-mentioned concept is called nested if…else statement.

Example 1: if..else condition

public class NestedIf { public static void main(String args[]){
int num_val=85 if( num_val < 100 ){ System.out.println("Number selected is not greater than 100"); if(num_val >= 50){ System.out.println("The given value is greater than or equal to 50"); }}

Example 2: if..else condition


Here’s a program that uses the nested if…else statement to get the largest of three values.

class Main { public static void main(String[] args) { // declaring float type variables float number_one = -2.4, number_two = 3.8, number_three = -4.7, largest; // ascertain if number_one is greater than or equal to number_two if ( number_one >= number_two) { // if...else statement inside the if block // checks if number_one is greater than or equal to number_three if ( number_one >= number_three) { largest_val = number_one; } else { largest_val = number_three; } } else { // if..else statement inside else block // checks if number_two is greater than or equal to number_three if ( number_two >= number_three) { largest_val = number_two; } else { largest_val = number_three; } } System.out.println("Largest Number: " + largest_val); }


We’ve allocated the values of variables ourselves in the programs above to make things easier. On the other hand, these numbers may come from user input data, log files, form submission, and other sources in real-world applications.

The if statement is useful in executing a code block on the off chance that a specified condition is true. On the other hand, the if…else statement is used alongside an if statement to run code if the evaluation of the condition is false. The if…else…if statement is also used to test multiple conditions.

Now you’re ready to use these Java conditional statements like a pro! This tutorial demonstrated how to use if, if…else, and if…else…if statements to control the flow of your Java programs using examples.


You might also like this video