Boolean CS Thinking Example 2
Follow the full solution, then compare it with the other examples linked below.
Example 2
mediumA variable `isLoggedIn` is a boolean. Write pseudocode that outputs 'Welcome back' if logged in, or 'Please log in' otherwise.
Solution
- 1 Step 1: IF isLoggedIn == TRUE THEN OUTPUT 'Welcome back'.
- 2 Step 2: ELSE OUTPUT 'Please log in'.
- 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
Example 1 easy
Evaluate each expression when x = 7: (a) x > 5, (b) x == 10, (c) x != 7, (d) x >= 7.
Example 3 easyWhat is the value of each: (a) NOT TRUE, (b) (5 < 3) OR (10 > 8), (c) (4 == 4) AND (3 > 5)?
Example 4 mediumA theme park ride requires: age β₯ 12 AND height β₯ 140. A child is age 13 and height 135. Evaluate th