Java If Statement

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

Java If Statement Syntax

If statement in Java Programming language has a simple structure that returns true or false:

if (test condition)  {
 
    Statement1;
    Statement2;
    Statement3;
    ………….
    ………….
    StatementN;
}

From the above code snippet, When the test condition is true, then the Statement1, Statement2, Statement3, ……., StatementN will be executed. Otherwise, all of them inside the Java if statement will skip. Let us see the flow chart for a better understanding.

If Statement Flow Chart

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 STATEMENT1 is executed, followed by STATEMENTN. While the expression evaluates to False, then the STATEMENTN 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.

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 function) 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 when 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

This If condition 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 functions 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

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

Comments are closed.