Java if..else Statement

60
views

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

Java if..else statement can also be called as control flow statements along with if, 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..else statement.

if..else Statement

if..else statements are the secondary path of execution when the condition in the if clause evaluated to be false.

Or you can say that if the condition in the if the clause is evaluated to be false then the statements in the else block are executed.

The else block comes immediately after the end of if block.

Syntax:

if(condition){
//Code to executed if condition is true
}else{
//Code to executed if condition is false
}

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..else Statement Flowchart
Java if..else Statement Flowchart

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

Program to check whether the number is even or odd using Java if..else Statement

public class IFElseDemo{

     public static void main(String []args){
        int number=11;
        if(number%2==0){
            System.out.println("Number is even");
        }
        else{
            System.out.println("Number is odd");    
        }
     }
}

Output

Number is odd

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
IF STATEMENT
NEXT
JAVA IF..ELSE..IF LADDER