Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

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("\n You have entered POSITIVE Number");
		}
	}

}

From the screenshot below, 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 ({}}.

Java If Statement 1

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).

Java If Statement 2

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.

Java If Statement 4

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.

Filed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy