Site icon TestingDocs.com

Write a java program with if-else statement?

Overview

If-else statement is a conditional statement. If the expression is true, then the true block is executed and if the expression is false, then the else block is executed. Let’s write a simple java program making use of the if-else statement.

Java Program

import java.util.Scanner;

//********************************
//IfElseDemo.java
//********************************
public class IfElseDemo {

  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int a,b;
    System.out.print("Enter a := ");
    a = keyboard.nextInt();
    System.out.print("Enter b := ");
    b = keyboard.nextInt();

    if(a>b) {
      System.out.println("a(" + a + ") is bigger than b(" + b + ")");
    }else {
      System.out.println("b(" + b + ") is bigger than a(" + a + ")");
    }
  }
}

 

Program Output

Enter a := 27
Enter b := 36
b(36) is bigger than a(27)

Screenshot

 

Java Tutorial on this website: https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

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

Exit mobile version