The examples below use AP CSP Pseudocode. For Java, see Working with boolean variables.

AP CSP variables can store boolean values (true or false). The value of a boolean variable can be used as a boolean expresison.

Example 1

childAteLunch ← true

IF(childAteLunch)
{
    DISPLAY("Time for nap")
}
ELSE
{
    DISPLAY("Time for lunch")
}

The variable childAteLunch is initialized to true.

The conditional statement IF(childAteLunch) checks if the value of childAteLunch is true. Since the value is true, the code segment displays "Time for nap ". (The DISPLAY procedure is defined as displaying its parameter followed by a space.)

Although it would work, it is not necessary to write IF(childAteLunch) = true). The original condition already evaluates to true if the variable stores true and false if the variable stores false.

Example 2

grade ← RANDOM(0, 100)

earnedHonorRoll ← grade ≥ 90

IF( NOT earnedHonorRoll )
{
    DISPLAY("Try again next marking period")
}
ELSE
{
    DISPLAY("Congrats")
}

The variable grade is initialized to a random value in the range 0 ≤ grade ≤ 100.

The variable earnedHonorRoll is initialized to the result of evaluating the boolean expression grade ≥ 90. The expression grade ≥ 90 evaluates to either true or false depending on the value of grade. The variable earnedHonorRoll is set to either true or false.

The conditional statement IF( NOT earnedHonorRoll ) checks if the value of earnedHonorRoll is false.

NOT false evaluates to true.
NOT true evaluates to false.

Although it would work, it is not necessary to write IF(earnedHonorRoll = false). The original condition already evaluates to true if the variable stores false and false if the variable stores true.

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Working with boolean variables in AP CSP Pseudocode