Java Code Example: simple if-else statement

In this Java program, a variable of type boolean is declared and initialized with the value false. The “!” character in front of a boolean variable inverts the expression. The if statement checks whether the variable isSet is false. If such a case should arise, the variable is set to true.

Code Explanation

3Initializes the variable isSet of type boolean with the value false
5 – 6If the value of the variable isSet is not true, the variable is set to true. So if the variable isSet was initialized with the value false, the value is set to true. If the boolean variable is true, the if statement is not executed because the condition is not met. So in any case true will be output.
8Outputs the value of the variable
public class SimpleIfExample {
    public static void main(String[] args) {
        boolean isSet = false;

        if (!isSet)
            isSet = true;