Java If Statement

In real-world programming, the Java If Statement is one of the most useful decision-making statements. The Java if statement allows the compiler to test the condition first and, depending upon the result, it will execute the statements. If the test condition is true, then only statements within the if statement will run.

Java If Statement Syntax

If statement in Java Programming language has a simple structure:

if (test condition)  {
 
    Statement 1;
    Statement 2;
    Statement 3;
    ………….
    ………….
    Statement n;
}

From the above code snippet, If the test condition inside the If statement is true, then the statements (Statement 1, Statement 2, Statement 3, ……., Statement n) will be executed. Otherwise, all these statements inside the Java if statement will skip. Let us see the flow chart for a better understanding.

Flow Chart of a Java If Statement

The following picture will show the flow chart behind this Java If Statement

 Flow Chart for Java If Statement

If the test condition is true, then STATEMENT 1 is executed, followed by STATEMENT N. If the condition is False, then the STATEMENT N will run. Because it is out of the if condition block and it has nothing to do with the condition result, let us see one example for a better understanding.

Java If Statement example

This Java if statement program allows the user to enter any positive integer, and it will check whether a number is Positive or Not using the if statement.

/* Java If Statement example */
package ConditionalStatements;

import java.util.Scanner;

public class IfStatement {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number;
		sc = new Scanner(System.in);		
		System.out.println("Please Enter any integer Value: ");
		Number = sc.nextInt();
		
		if (Number > 1) {
			System.out.println("\nYou have entered POSITIVE Number");
		}
	}

}

You can observe that we entered 25 as Number, and this program will check whether 25 is greater than 1 or not. As we all know that it is True, it is printing (System.ot.println statement) inside the curly brackets ({}}.

Please Enter any integer Value: 
25

You have entered POSITIVE Number

Java If condition does not require the curly brackets to hold a single line, but for multiple or group of code lines, it is mandatory. It is always good practice to use curly brackets following the If statements. Let us change the value to check what happens if the Java condition fails? (number < 1).

Please Enter any integer Value: 
-5

It prints nothing because we have nothing to print after the if statement block. I guess you are confused about the result. Let us see one more example.

Java If Statement example 2

The If statement program allows you to enter any positive integer, and it will check whether a number is Positive or Not.

package ConditionalStatements;

import java.util.Scanner;

public class IfStatement {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number;
		sc = new Scanner(System.in);		
		System.out.println(" Please Enter any integer Value: ");
		Number = sc.nextInt();
		
		if (Number > 1) {
			System.out.println("You have entered POSITIVE Number");
		}
		System.out.println("This Message is coming from Outside the IF STATEMENT");
	}
}
Java If Statement 3

As you can observe from the above output, Java printed both the System.ot.println statements because 23 is greater than 1. Let us try the negative values to fail the condition deliberately.

 Please Enter any integer Value: 
-50
This Message is coming from Outside the IF STATEMENT

Here, Condition inside the If statement failed (number < 1). It prints nothing from the If statement block, so it printed only one System.ot.println statement, which is outside the statement block.

Comments are closed.