If Else Condition #
Java supports logical conditions in few different ways. One of them is if
- else
statement. They are used to direct
the flow of the program. For example if
age is less than 18 restrict access else
give access.
if (age < 18) {
System.out.println("Restrict access.");
}
else {
System.out.println("Do something, like voting.");
}
It is possible to have multiple if
statements by using else if
. For example if
age is 2 the code below will print
only the Baby
. The Teen
will be printed only when the if
condition is not met and the age is less than 18.
Otherwise Adult
will be printed.
if (age < 3) {
System.out.println("Baby");
}
else if (age < 18) {
System.out.println("Teen");
}
else {
System.out.println("Adult");
}