An If...Else statement follows the following structure
If (condition_to_test) {
//result if condition is true
}
Else {
//result if condition is false
}
The program below tests whether a user visiting a site has attained the minimum age required which is 18 years of age, thus this becomes the (condition_to_test). If the test returns a true value then the user is allowed to proceed to the website, otherwise, access is denied.
public class IfElseExample {
public static void main (String[] args) {
int user=17;
if (user<=18) {
System.out.println("User cannot proceed to view the website");
}
else {
System.out.println("User is allowed to visit website");
}
}
}
Do'stlaringiz bilan baham: |