C++ Code Example: simple if-else statement

In this C++ 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

5Initializes the variable isSet of type bool with the value false
7 – 8If 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.
10Boolean variables in C++ can have the values 0 (for false) and 1 (for true). In the shortened if statement, the string true or false is output depending on the value.
#include <iostream>
using namespace std;

int main() {
    bool isSet = false;

    if (!isSet)
        isSet = true;

    cout << (isSet == 1 ? "true" : "false");

    return 0;