Site icon TestingDocs.com

Java if-else statement with Examples

Overview

Java if-else is a control structure and is used to make decisions based on conditions. We can use this statement to branch out code based on branch conditions.

Java if-else statement

If the given condition is True the program executes one path, and if the condition is false the program takes another path.

Let’s see a simple example, below a sample piece of  Java code:

public class JavaIFExample 
{
  
  public static void main(String[] args)
   {
    
    int dummyVariable = 70 ;
    
    if(dummyVariable > 100)
    { 
      System.out.println("dummyVariable > 100 ");
    } 
    else 
    { 
      System.out.println("dummyVariable < 100 "); }
   	           
   }

}

 

Output of the Program

dummyVariable < 100

 

 

The program output depends on what the if condition evaluates to True or False. The program executes the code depending on the value of dummyVariable in the above program.

 

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version