The example below demonstrates 'if' and 'if/else' statements and Java
boolean type.
//the program demonstrates if/else statement
public class IfElse {
public static void main(String[] args) {
int x = 5;
int y = 8;
// if statement
if (x < y) {
System.out.println("x < y");
}
// evaluating a boolean expression
boolean xLessThanY = x < y;
System.out.println("xLessThanY = " + xLessThanY);
// if/else statement
if (x == 4) {
System.out.println("x is 4");
} else {
System.out.println("x is not 4");
}
int z = 0;
// boolean expressions; nested if/else
if ( x == 4 || y < 10) {
if ( x <= 5 && y >= 8.05) {
z = 1;
} else {
z = 2;
}
} else {
z = 3;
}
System.out.println("z = " + z);
}
}
This is an example from CSci 2101 course.