Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Programming Fundamentals · Grade 6-8

Boolean

A data type representing a logical value that can only be true or false—nothing else.


Quick Answer

A data type representing a logical value that can only be true or false—nothing else. You use booleans to record yes/no facts, such as whether the game is over, the user is logged in or the door is unlocked, and to make decisions: comparisons such as score > 100 or name == 'Ana' produce a boolean, and if statements and while loops read one to choose what happens next. Storing an answer as True or False rather than as the text 'yes' keeps conditions short and safe from spelling slips like 'Yes' versus 'yes'.

Why This Matters

Every decision a program makes comes down to a boolean: whether to show the Game Over screen, open a door, or accept a login. Once you see that score > 100 is itself a value, you can store it under a clear name like passed and reuse it. Without that, code fills up with repeated comparisons, and with bugs like if x = 5, which changes x instead of testing it.

Intuitive Explanation

A boolean is the answer to a yes/no question, stored as a value. Ask whether 7 is greater than 3 and the program writes down True; ask whether 'cat' equals 'dog' and it writes down False. Those answers can be saved like any other value: passed = score >= 50 stores True when score is 62 and False when score is 41. The variable now holds the result of a question, not a number.

Because a boolean already is a yes or a no, it plugs straight into an if statement or a while loop. if passed: print('Well done') runs the print only when passed is True. while not game_over: keeps a game running until something sets game_over to True. Writing if passed == True also works, but it only asks whether the answer to the question is yes; if passed says the same thing directly.

Two things look like booleans and are not. The string 'False' is five letters of text, and in Python any non-empty string counts as true in a condition, so if 'False': runs its body. And a single = is assignment, not a question: x = 5 stores 5, while x == 5 asks a question and produces True or False. Only the second belongs in a condition.

Think of it like this. A checkbox on a form is a boolean: ticked or empty, with no third state, and anyone can read it later without asking the question again. A comparison like age >= 13 is the person deciding whether to tick it; the if statement is the office that reads the box. The picture stops at Python's truthy values, where 0 or empty text also counts as an empty box.

Core idea. Booleans are the language of decisions. Every if-statement evaluates a boolean condition.


When to Use

Use a boolean when a value can only be yes or no: is the level complete, has the player paid, is the input valid. Comparisons (==, !=, <, >, <=, >=) always produce one, so store a comparison's result in a variable when you will test it more than once. Signal words are is, has, can and whether. If there are more than two possibilities, such as small, medium and large, use a string or an integer. Combining several yes/no facts is a job for logical operators.

Tip. Ask: Can the value only be yes or no, or is it the result of a comparison that an if or while will read?

How to Recognize It

Look at what the value can be and what reads it: a yes-or-no fact, the answer to a comparison, or something with more than two options.

  1. Are there exactly two possible states, such as on or off, or logged in or not?

    Yes means a Boolean fits. With three or more states, such as 'easy', 'medium' and 'hard', a String or an Integer code is the better choice.

  2. Is the value produced by a comparison such as age >= 13 or name == 'Ana'?

    Yes means the result is a Boolean, True or False, ready to store or to test. A single = in that place would store a value instead of comparing.

  3. Are two or more true-or-false facts joined with and, or, not?

    Yes means you are using Boolean Logic, the rules for combining these values; the inputs and the result are still Booleans.

  4. Is the value written in quotes, like 'True' or 'yes'?

    Yes means it is a String that only looks like a Boolean, and in Python even 'False' counts as true in an if, because the string is not empty.

Boolean vs Boolean Logic vs Selection vs String

True and false appear in all four, so a stored value, the rules for combining values, the if that reads them and text that only spells them get treated as one idea. Ask whether you are holding a value, combining values, or acting on one.

Boolean

Meaning
A Boolean is a single value that is either True or False, stored directly or produced by a comparison.
Key test
One true-or-false value, such as game_over or the result of x > 3.
Example
door_open = False stores one fact; later, door_open = True changes it.

Boolean Logic

Meaning
Boolean logic is the set of rules for combining true and false values with AND, OR and NOT.
Key test
Two or more Booleans are combined into one answer.
Formula
¬(A∧B)=¬A∨¬B
Example
is_weekend or is_holiday is True when at least one of the two is True.

Selection

Meaning
Selection is the if statement that reads a Boolean and runs one block of code or another.
Key test
Something different happens depending on the value; the if itself is not a value.
Example
if raining: take_umbrella() makes the call only when raining is True.

String

Meaning
A string such as 'True' or 'yes' is text, even when its letters spell out a truth value.
Key test
Quotation marks; 'yes' and 'Yes' are different strings, and both count as true.
Example
answer = 'no' still makes if answer: run in Python, because the string is not empty.

Formula & Notation

Worked Examples

Example 1 — Store a comparison, then use it

Easy

A game has score = 62 and lives = 0. It runs passed = score >= 50 and game_over = lives == 0. What do passed and game_over hold, and does if passed and not game_over: print('Next level') print anything?
Show the steps and the answer
  1. score >= 50 means 62 >= 50, which is True, so passed = True.

    A comparison produces a boolean, and assignment stores it like any other value.

  2. lives == 0 means 0 == 0, which is True, so game_over = True.

    The double equals asks the question; the single equals in front stores its answer.

  3. not game_over is not True, which is False.

    not flips a boolean to the other value.

  4. passed and not game_over is True and False, which is False, so nothing is printed.

    and needs both sides True; the player reached the score but has no lives left.

Answer: passed = True, game_over = True, and nothing is printed.

Takeaway: Comparisons create booleans you can store under clear names; conditions then read those stored answers.

Example 2 — The text 'False' is not False

Standard

A settings file is read into sound_on = 'False' (with quotes). The program runs if sound_on: play_music(). Does the music play? How should the setting be stored?
Show the steps and the answer
  1. Check the type: 'False' in quotes is a string of five characters, not a boolean.

    Quotation marks make text, whatever the word inside says.

  2. Evaluate the condition: in Python a non-empty string counts as true, so if 'False': runs its body and the music plays.

    Only the empty string '' counts as false; the program follows the type rules, not the meaning of the word.

  3. Store a real boolean instead: sound_on = False, with no quotes.

    A boolean value has exactly two states, so the condition reads it correctly.

  4. If the setting must come from text, convert it: sound_on = (text == 'True').

    text == 'True' is a comparison, so it produces the boolean True only when the stored text is exactly 'True'.

Answer: The music plays, because 'False' is a non-empty string. Store sound_on = False, or convert the text with sound_on = (text == 'True').

Takeaway: A boolean is True or False without quotes; the word 'False' in quotes is text, and in Python it counts as true.

Example 3 — Find the mistake: one equals sign

Application

A quiz should say 'Perfect' only when answer is 10. Kai writes in JavaScript: if (answer = 10) { console.log('Perfect'); }. With answer = 7 the program still prints Perfect. Find the mistake and correct it.
Show the steps and the answer
  1. Read answer = 10 as what it is: an assignment that stores 10 in answer.

    A single = always stores a value; it never asks a question.

  2. Trace with answer = 7: the condition stores 10, so answer is now 10, and the condition's value is 10.

    In JavaScript an assignment gives back the value it stored, and the if statement tests that value.

  3. 10 is a nonzero number, which JavaScript treats as true, so Perfect prints and the student's 7 has been overwritten.

    Every nonzero value passes, so the bug fires whatever the student answered.

  4. Fix: if (answer === 10) (or answer == 10). Retrace: 7 === 10 is false, so nothing prints and answer stays 7.

    A comparison produces a boolean without changing the variable.

Answer: Use if (answer === 10). With answer = 7 the comparison is false, nothing prints, and answer keeps its value 7.

Takeaway: A condition needs a comparison that produces a boolean; a single = stores a value instead, and in JavaScript or C that bug runs without any error message.

Where You'll See It

  • A light switch

    A switch is either on or off with nothing in between — exactly like a boolean that can only be true or false.

  • Is the game over?

    A game checks a boolean like game_over each frame; while it is false the game keeps running, and the instant it becomes true the 'Game Over' screen shows.

  • Login check

    An app stores logged_in as true or false; the if-statement that shows your profile only runs when that boolean is true.

Common Mistakes

  1. The slip: You should write if (x == True) to check a boolean.

    Why it's tempting: It reads naturally as 'if x is true', so comparing to True looks more explicit and clear.

    Instead: A boolean already is a condition, so write if (x). Comparing to True is redundant and can even break with truthy non-boolean values.

  2. The slip: Only the value true or false counts as 'true' in an if-statement.

    Why it's tempting: Booleans are defined as just true/false, so it seems like nothing else can drive a condition.

    Instead: Conditions also accept truthy/falsy values: 0, '', None, and [] act as false, while non-zero numbers and non-empty strings act as true.

  3. The slip: if (x = 5) tests whether x equals 5.

    Why it's tempting: A single = looks like 'is', and 'x equals 5' is what you meant to say.

    Instead: = is assignment (it sets x to 5); == is the comparison that produces a boolean. Use if (x == 5) to test.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. x = 7. Evaluate x > 5, x == 10 and x != 7.

    Hint: Each comparison gives True or False.

  2. is_raining = True and has_umbrella = False. What is is_raining and not has_umbrella?

    Hint: Work out not has_umbrella first.

  3. Which of these count as False in a Python if-statement: 0, 1, '' (empty string), 'False', [] (empty list)?

    Hint: Zero and empty things are falsy. Is the string 'False' empty?

  4. age = 15 and can_vote = age >= 18. What type is can_vote, and what is its value?

    Hint: A comparison produces a value of which type?

  5. light_on records whether a lamp is on. brightness records a level from 0 to 100. Which one should be a boolean?

    Hint: How many possible values does each one need?

  6. A JavaScript student writes if (score = 100) { win(); } and finds that every player wins. What is wrong?

    Hint: Is = a test or a store?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why are Boolean variables often named is_open or has_key?

A name like that reads as a yes-or-no question, so the code reads almost like a sentence: if is_open: or while not has_key:. A name like status leaves the reader guessing whether it holds True, the word 'open' or a number. Many style guides suggest starting Boolean names with is, has, can or should, and avoiding negative names like not_done, because if not not_done: is hard to follow.

What if the answer is not known yet, neither yes nor no?

A Boolean cannot hold a third state, but real data often needs one, such as a survey question nobody has answered. Python programs often use None for 'unknown', and databases use NULL. The trap is that if answer: treats None like False, so a missing answer looks like a no. Testing answer is None first keeps 'unknown' apart from 'no'.

What does 3 < x < 10 mean in code?

In Python it means what it means in math: x is between 3 and 10, the same as 3 < x and x < 10. Most other languages do not read it that way. In JavaScript, 3 < x is worked out first and gives true or false, which is then compared with 10 as 1 or 0, so 3 < 50 < 10 comes out true. Writing both comparisons joined by and is safe in every language.

Learning Path

Boolean

You are here

You need Assignment (storing values) and Data Types (each value has a kind) first. Boolean adds a type with exactly two values, produced by comparisons and read by conditions. Next, Boolean Logic combines true/false values with AND, OR and NOT, and Selection uses a boolean to choose which block of code runs.

See Also