Java If-else Statement

This is the most basic flow control statement that uses logical operators to determine whether or not a specific condition is fulfilled. In every program there is a point when we need to execute a set of statements based on a condition then we use control statement explanations.

When we talk about Java then Java if Statement is used to check the condition. In java there are four types of if statements in java:

  • If statement
  • If-else statement
  • If-else-if statement
  • Nested-if statement
Java If statement

Java if statement check the condition. It executes if condition is true.

Syntax

If (condition){

//code execute

}

It shows that when the condition is true then the statement is executed.

Java If-else

>

Example of if statement

public class IfStatementExample {

public static void main(String args[]){

int age=25;

if( age < 30 ){

/* This println statement will only execute,

* if the above condition is true

*/

System.out.println(“Age is less than 30 eligible for exam”);

}

}

}

Output

Age is less than 30 eligible for exam

Java If-else statement

In java if-else statement, it also check condition.

Syntax

if(condition) {

//code execute

}

else {

//code execute

}

The statement in “if” will execute if the condition is true, and the statement in “else” will execute if the condition is false.

Java If-else

>

Example of if-else statement

public class IfElseExample {

public static void main(String args[]){

int Age=16;

if( Age > 18 ){

System.out.println(“Age is less than 18”);

}

else {

System.out.println(“Age is greater than or equal to 18 are eligible for voting”);

}

}

}

Output

Age is greater than or equal to 18 are eligible for voting

If-else-if statement

The if-else-if statement is used when we have multiple condition to check. It also known as if

else if ladder.
Syntax

if(condition_1) {

/*if condition_1 is true execute this*/

statement(s);

}

else if(condition_2) {

/* execute this if condition_1 is not met and

* condition_2 is met

*/

statement(s);

}

else if(condition_3) {

/* execute this if condition_1 & condition_2 are

* not met and condition_3 is met

*/

statement(s);

}

.

.

.

else {

/* if none of the condition is true

* then these statements gets executed

*/

statement(s);

}

Example of if else if Statement

public class IfElseIfExample {

public static void main(String[] args) {

int marks=65;

if(marks<50){

System.out.println(“fail”);

}

else if(marks>=50 && marks<60){

System.out.println(“D grade”);

}

else if(marks>=60 && marks<70){

System.out.println(“C grade”);

}

else if(marks>=70 && marks<80){

System.out.println(“B grade”);

}

else if(marks>=80 && marks<90){

System.out.println(“A grade”);

}else if(marks>=90 && marks<100){

System.out.println(“A+ grade”);

}else{

System.out.println(“Invalid!”);

}

}

}

Output

C Grade

Nested-if statement

In Nested-If statement when there is if statement inside another if statement then it is known as nested if statement.

Syntax

if(condition_1) {

Statement1(s);

if(condition_2) {

Statement2(s);

}

}

In Nested-if statement, Statement1 will execute when condition1 is true and statement2 will execute when both the conditions (condition_1 and condition_2) are true.

Example of Nested if statement

public class NestedIfExample {

public static void main(String args[]){

int num=70;

if( num < 100 ){

System.out.println(“Number is less than 100”);

if(num > 50){

System.out.println(“Number is greater than 50”);

}

}

}

}

Output

Number is less than 100

Number is greater than 50

Copyright 1999- Ducat Creative, All rights reserved.