refresh java logo

if, if-else Statement in Java

Sometime as a programmer you want to execute one or more lines of code on basis of certain condition. For an example, you may want that if a number num1 is greater than 0, print a message in console as "num1 is a positive number" by executing code System.out.println("num1 is a positive number"); To accomplish such scenarios, java provide a keyword known as if keyword which allows programmers to execute a set of statements on certain condition. if statement asks the computer to execute the instructions given inside it's block only when the condition is true. The syntax of if keyword is:

 if(condition)
    {
     // code to be executed inside if block
    }

Here condition is a boolean expression which returns either true or false. If it returns true, the code written inside if block get's executed and if it returns false, if block code will not be executed. Expression could be like a>0, (a+b)>10 (a-b)>0 etc. which returns either true or false.

What is if block ?

Everything that comes inside matching curly brackets {} after if keyword is the part of if statement which is also called as if block.

Control flow diagram of if Statement

if statement in java

Java Program of if Statement

 class IfStatement
   {
     public static void main(String [] args)
        {          
           int num1 = 20; 
           if(num1 > 0)
             {
               System.out.println("num1 is a positive number");
             }  
           if(num1 < 0)
             {
               System.out.println("num1 is a negative number");
             }            
            System.out.println("code after if block");                   
         }
   }

Output :

num1 is a positive number
code after if block

if-else (if-then-else) Statement

Sometime as a programmer you want to execute a set of statements when a condition is true and if that is not true then you want to execute some other set of statements. To accomplish this java provides if then else statement. else block get's executed only when the condition in if statement is false. Programmer can also read this as "if something is true execute this else execute that". The syntax of if-then-else statement is:

 if(condition)
   {
    // code to be executed inside if block
   }
 else
   {
    // code to be executed inside else block
   }    

Can I use else statement without if statement ?

No, you can not use else statement without if statement. else statement must be used along with if statement.

Control flow diagram of if-else Statement

if-else statement in java

Java Program of if-else Statement

 class IfElseStatement
   {
     public static void main(String [] args)
        {          
           int num1 = 20, num2 = 15; 
           if(num1 == num2)
             {
               System.out.println("num1 is equal to num2");
             }
            else
             {
               System.out.println("num1 is not equal to num2");
             }  
            System.out.println("code after if else block");                   
         }
   }

Output:

num1 is not equal to num2
code after if else block

Can I use numbers in if condition like if(10), if(20.5) etc ?

No, you can not use numbers in if condition because java compiler expects only boolean value in if condition.

★★★
  • Java if else statements are also known as decision making or conditional statements.
  • Ensure that all letters of conditional statements are small. For example If, Else, IF are not valid java keywords.
  • Do not add semicolon(;) after if, like if(); which will end the if statement there itself.
  • If no curly brackets {} is used with if keyword, only first line of code after if keyword will be considered as part of if statement.