If Statement in Java | Example Program

An if statement in Java is the simplest decision-making statement that allows to specify alternative paths of execution in a program.

It is also called conditional control statement or selection statement in Java.

In simple words, if statement executes a set of statements when a condition is true. It is used to change the flow of the program.

If statement in Java consists of a boolean expression followed by one or more statements. The general syntax is as follows:

if(Condition)
   statement;
or,
  if(Condition) 
  {
    statements;
  }

Here,

a. Condition is a boolean expression that returns a boolean value either true or false.

b. Condition can also include any relational operators to form Boolean expressions that must be enclosed within parentheses.

c. If the condition is true then the statement is executed. If the condition is false then the statement is bypassed.

d. If the block of if statement contains more than one statement, must enclose these statements within a pair of braces ({ }).

Look at the below figure where you can see the if conditional flowchart diagram.

Java if statement flowchart diagram

Let’s understand if statement with the help of an example. Consider the following program code given below.

float myPer = 92;
if(myPer >= 80)
    System.out.println("Grade A");

If the boolean expression (condition) evaluates to true then the statement will be executed and print the message “Grade A” on the console. If the boolean expression evaluates to false then the statement will not be executed.


Similarly, consider the following program code.

if (radius >= 0) 
{
 area = radius * radius * PI;
 System.out.println("Area for circle of radius " +radius + " is " + area);
}

If the boolean expression evaluates to true, statements in the block will be executed.

That is, if the value of radius is greater than or equal to 0, the area of circle will be computed and the result will be displayed on the console otherwise, the two statements in the block will be skipped, not executed, and continues with the program.

Some Valid if statements are as follows:

1. int x = 1;
   if(x > 0)
      System.out.println(a+ " is a positive number");

2. int x = 1;
    if(x) // Same as : if(a != 0)
    System.out.println(a+ " is a nonzero number");

3. int x = 10, y = 10;
   if(x == y)
     System.out.println("x and y are equal number");

4. int x = 5, y = 10;
   if(x < y){
     System.out.println("x is less than y");
   }

5. boolean x = true;
   if(x) // Boolean value is used to whether the condition is true or not.
    System.out.println("You are eligible for voting");

Types of Selection Statements in Java


Java supports several types of selection statements that are as follows:

  • One way if statements
  • Two-way if-else statements
  • Nested if statements
  • Multi-way if-else statements
  • Switch statements
  • Conditional expressions

Example Program based on If Statement in Java


Let’s take some example programs based on if statement in Java.

1. Let’s create a program where we will input marks for three subjects such as maths, chemistry, and physics. Then we will calculate the percentage of three subject marks and display “Grade A” if the percentage is greater than or equal to 85.


Look at the program source code to understand better.

Program source code 1:

package javaProgram;
import java.util.Scanner;
public class Test {
public static void main(String[] args) 
{ 
// Create an object of Scanner class to take inputs of three subjects.	
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter your physics marks:");
  int phyMark = sc.nextInt();
 
System.out.println("Enter your chemistry marks:");
  int chemMark = sc.nextInt();
  
System.out.println("Enter your maths marks:");
 int mathsMark = sc.nextInt();
 
float total = phyMark + chemMark + mathsMark;
float myPer = total/3;
System.out.println("Your percentage: " +myPer);

if(myPer >= 85.0) // if the condition is true then statement will be displayed.
 	System.out.println("Grade A"); 
 }
}
Output:
      Enter your chemistry marks:
      95
      Enter your maths marks:
      100
      Your percentage: 97.666664
      Grade A

2. Let’s create a program to prompt the user to enter a number. If the number is divisible by 2, the program displays a message “number is divisible by 2”. Look at the source code to understand better.

Program source code 2:

package javaProgram;
import java.util.Scanner;
public class Test {
public static void main(String[] args) 
{ 
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter an integer number:");
  int num = sc.nextInt();
  
if(num % 2 == 0)
  System.out.println(num +" is divisibe by 2");
 
if(num % 2 != 0)
  System.out.println(num + " is not divisible by 2");
 }
}
Output:
      Enter an integer number:
      98
      98 is divisibe by 2

Use of Logical Operators in If Statement


Logical operators can be used in the expression when we want to check multiple conditions together. There are three types of logical operators in Java.

  • && (AND)
  • || (OR)
  • ! (Not)

Let’s take some example programs based on logical operators used in the if statements.

Program source code 3: Logical AND (&&)

package javaProgram;
public class Test {
public static void main(String[] args) 
{ 
 int x = 20, y = 40, z = 50;
 if((y > x) && (y < z)) // 
   System.out.println("y is greater than x but smaller than z");
 
 if((x < y) && (y < z))
   System.out.println("z is greater than x, y");
 
 if((x < y) && (y < z))
  System.out.println("x is smaller than y, z");	 
 }
}
Output:
       y is greater than x but smaller than z
       z is greater than x, y
       x is smaller than y, z

In the preceding example program, statements 6, 9, and 12 will produce true only if both expressions are true. If any of the expressions is false or both expressions are false, statements 6, 9, and 12 will produce false.

Program source code 4: Logic OR (||)

package javaProgram;
public class Test {
public static void main(String[] args) 
{ 
int x = 20, y = 10, z = 40;	
boolean value;
if(value = (x > y) || (y < z))
  System.out.println(value);	 

if(value = (x > y) || (y > z))
   System.out.println(value);	

if(value = (x < y) || (y < z))
   System.out.println(value);

if(value = (x < y) || (y > z))
   System.out.println(value);
  }
}
Output:
       true
       true
       true

In the above example program, if any of the expressions are true or both expressions are true then it will execute the true part. If both expressions are false then the statement will not be executed.

Program source code 5: Logical Not (!)

package javaProgram;
public class Test {
public static void main(String[] args) 
{ 
int x = 20, y = 10;	
boolean value;
if(value = (x == 20) && (y != 20))
  System.out.println(value);	 
 }
Output:
       true

We can combine several expressions by using “Logical Not” operators. The result will be a boolean type.


Hope that this tutorial has covered all the important points related to Java if statement with example programs. I hope that you will have understood the basic syntax of if statement.

In the next tutorial, we will learn if-else statements in Java with example programs.
Thanks for reading!!!
Next ⇒ If-else statement in Java

⇐ Prev Next ⇒