If – else statement in java with example



The if – else statement is an extension of the simple if statement.

Syntax:-

if(condition)
{
True - Block of Statement
}
else
{
false - Block of Statement
}
Statement;

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
# Either true – block of statement or false – block of statement is executed, not both.

Execution:-

If condition is true than true - block of statement is executed otherwise false - block statement is executed than control is moved to statement next to if statement.

Example:-

Program to find Greatest number between 2

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num1, num2;
Scanner grest = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = grest.nextInt();
System.out.println("Enter 2nd number");
num2 = grest.nextInt();
if(num1 <  num2)
System.out.println("Greatest number"+num1);
else
System.out.println("Greatest number"+num2);
}
}

Leave reply

Add your comments here

Back to Top