Skip to content
Home » Blog » Java if statement

Java if statement

Java if Statement Tutorial

In this tutorial, you’ll learn about Java if statement and how to use it in Java Programs.

Java if statement can also be called as control flow statements along with if else, if else ladder and switch statement these are used to control the flow of program based on given conditions, so let’s understand Java if statement.

IF Statements

If Statements are the most basic among all control flow statements. It stated that if the expression or condition in the if the clause is evaluated as true then only a certain section of code is to be executed.

Here, the expression is Boolean expression i.e. it returns either true or false.

Syntax:

if(condition){
//section of code to be executed
}

If the condition is evaluated to be false then control of program would directly jump to end of if statement. The  flowchart for if statement can be shown as:

Java if Statement flowchart
Java if Statement flowchart

for the better understanding of Java if Statement below are some examples to illustrate the use of if Statement in Java.

Program to check if the number is greater than 5 or not

public class IfDemo{
public static void main(String []args){

int x=10;
         if(x>5) //Condition is evaluated to true
                      {
          //statements to be executed if the condition is true
          System.out.println("x is greater than 5");
         }
    }
}

Output:

x is greater than 5

However, the opening and closing braces are optional, provided there is only one statement to be executed.

as in the above program, you can add addition conditional statement.

Above example can also be written as :

public class ifDemo{
public static void main(String []args){
        	int x=10;
        	if(x>5) //Conditon is evaluated to true
            	    System.out.println("x is greater than 5");
     }
}

Output:

x is greater than 5

Ask your questions about Java control flow if statement and clarify your/others doubts by commenting. Documentation.

Please write to us at [email protected] to report any issue with the above content or for feedback.

This tutorial is contributed by Ashutosh Sahu

PREVIOUS
JAVA VARIABLE SCOPE
NEXT
JAVA IF..ELSE STATEMENT