java if else statement code example

Example 1: if else in java

import java.io.*;
public class JavaIfElse
{
   public static void main(String[] args)
   {
      int number = 15;
      // check if number is divisible by 2
      if(number%2 == 0)
      {
         System.out.println(number + " is even number");
      }
      else
      {
         System.out.println(number + " is odd number");
      }
   }
}

Example 2: if statement in java

int x = 3;

if (x<=3){
	System.out.println("Hello World");
}

else {
	System.out.println("Bye World");
}

Example 3: how to make if else statments in java

if (condition) {
  // code block
} else {
  // code block
}

Example 4: java else if statements

System.out.println("How many sattelites does JUPITER have ??");
		C = scan.nextInt();
		
		if (C == 79)
		{
			System.out.println("Congrats ! You got the Third  Question Right :)");
			points = points + 1;
		}
		
		else if (C != 79)
		{
			System.out.println("Sorry but your answer is wrong :(");
		}

Example 5: else statement java

int x = 3;

if (x == 3) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

Example 6: else if java

else if statement is used to specify new condition if first condition is false.
Syntax:

if(condition1)
{
   // execute if condition1 is true
}
else if (condition2)
{
   // execute if condition2 is true
}
else if (condition3)
{
   // execute if condition3 is true
}
else
{
   // execute if conditions 1, 2 and 3 becomes false
}

Tags:

Java Example