DSA Factory
Free lessonsProgramming basics · Stage 0 · Names for values · Step 4

True or false values

Ask a yes-or-no question about two values and keep the answer. About 7 minutes.

Questions with yes-or-no answers

Not every value is a number. Some answer a question with just two possibilities: true or false. We call these booleans.

You get one by comparing two values. Is the age at least 18? Is the price more than the money in your pocket? The comparison words are: bigger than, smaller than, at least, at most, the same as, and different from. Each one gives back true or false.

A comparison: asks a question and answers true or false.
In code
age = 20
adult = age >= 18      # True
Can we afford it?
money: 50
0
price: 30
1
enough: ?
2
line
money
50
price
30

money holds 50 and price holds 30.

Move 1 of 4

Keep the answer

A true-or-false answer can sit in a box like any other value. You can store it, pass it on, or give it back as the result of your function.

One warning: a single equals sign puts a value into a box. To ask whether two values are the same, you use two equals signs. Mixing them up is one of the most common beginner mistakes, so read each line carefully.

One equals sign: puts a value in. Two equals signs ask a question.
In code
enough = money >= price
same = money == price
Quick check

money is 20 and price is 30. What does the question money >= price give back?

  1. Afalse
  2. Btrue
  3. C10
Show the answer

false. 20 is not at least 30, so the answer is false.

Your problem

Can we afford it?

You have some money and want to buy something that costs a given price. Return true if you can afford it (you have at least the price), and false if not.

Example
money = 50, price = 30 → true

0 ≤ money, price ≤ 1,000,000

Solve it in your browserPython, C++, Java or JavaScript. Hints if you get stuck. No sign-up needed.
Start coding