If else statement in Java

If else statement is the conditional statement which are used to work on specific condition.

General Syntax for if-else

if(condition or expression) {

//body or statements

}

else {

//body or statements

}

If a condition is true, it will go inside the body of the if statement. If the condition is false it will go inside the body of the else statement.

Imagine as your mother says you to buy 1kg sugar from the nearest store. She also says that if it is not available there, get it from the store from where it available.

How will you solve this using programming ?

Imagine sugar_needed as one variable and available_in_store as another.

Solve like this below,

public class IfElseTest {

public static void main(String args[]) {

int sugar_needed=1,available_in_store=0;

if(sugar_needed<=available_in_store){

System.out.println("You can get from here");

}

else {

System.out.println("You can get from other store");

}

}

}

Try In Editor

Output

You can get from other store