An if…..else statement is a type of control flow statement in which there are two blocks of code. The first block of code, located inside the if statement, will be executed if the condition specified in the statement evaluates to true. If the condition is false, the second block of code, located inside the else statement, will be executed instead.
The syntax for an if…..else statement can be written as follows:
if (condition) {
//block of code
} else {
//block of code
}
Example:
public class JavaIf {
public static void main(String[] args) {
String name = "Mohan";
int Roll = 25;
if (name == "Mohan" && Roll == 26) {
System.out.println("Details of Mohan.");
} else {
System.out.println("Invalid details.");
}
}
}
Output:
Invalid details.