Boolean CS Thinking Example 2

Follow the full solution, then compare it with the other examples linked below.

Example 2

medium
A variable `isLoggedIn` is a boolean. Write pseudocode that outputs 'Welcome back' if logged in, or 'Please log in' otherwise.

Solution

  1. 1
    Step 1: IF isLoggedIn == TRUE THEN OUTPUT 'Welcome back'.
  2. 2
    Step 2: ELSE OUTPUT 'Please log in'.
  3. 3
    Step 3: Since isLoggedIn is already a boolean, we can simplify: IF isLoggedIn THEN ... (no need for == TRUE).

Answer

IF isLoggedIn THEN OUTPUT 'Welcome back' ELSE OUTPUT 'Please log in'.
Boolean variables can be used directly in conditions without comparing to TRUE. This makes code cleaner and is considered good practice.

About Boolean

A data type representing a logical value that can only be true or falseβ€”nothing else. Booleans are produced by comparison operators (==, <, >, !=) and consumed by control structures (if-statements, while-loops) to make decisions.

Learn more about Boolean β†’

More Boolean Examples