Operators
The verbs — add, compare, combine, decide.
Prerequisites:VariablesData Types
Story — the cook on the kitchen counter
A cook stands at the counter with two ingredients in front of them and a row of tools above the cutting board. A knife (+), a measuring cup (==), a sieve (>), a blender (and). Same ingredients, different tools — and you get a different dish each time.
That is what an operator is. Take one or two values, apply a small operation, get a new value back. Some operators give numbers; some give a true-or-false answer (a boolean). The cook keeps the operands; the result is something new.
Cook says
See it — apply each tool
We hold a = 5 and b = 2 on the counter. Press Step to walk through the carousel of operators and watch the result chip change colour — green for numbers, blue for booleans.
Text-only trace
- a = 5, b = 2
Try it — order the recipe
Put the four lines in an order that runs.
Drag the pieces — or tab to one and use the ↑/↓ buttons — to put the program in order.
print(a + b)b = 2a = 5print(a > b)
Code it — five languages, one idea
The same six expressions in five popular languages. Notice how comparisons always come back as a boolean — the only thing that changes is whether your language prints them as true or 1.
a = 5
b = 2
print(a + b) # 7
print(a > b) # True
print(a == b) # False
print((a > 0) and (b > 0)) # TrueWalk through it line by line
Each click of Next applies one operator. Notice which lines produce a number and which produce a True/False.
1a = 52b = 23print(a + b)4print(a > b)5print(a == b)6print((a > 0) and (b > 0))
Quiz it — make it stick
No Dumb Questions
Real questions other learners asked on this page.
Why does C++ print `1` and `0` for booleans instead of `true` and `false`?
C++ historically stored booleans as integers, with 1 meaning true and 0 meaning false. Many languages still treat them that way under the hood. You can ask C++ to print "true"/"false" by setting `std::boolalpha`, but the default is the old numeric form.What is the difference between `=`, `==`, and `===`?
`=` assigns. `==` compares values. `===` (in JavaScript) compares values *and* types — `5 == "5"` is true, but `5 === "5"` is false. Other languages do not need `===` because they would not compare a number to a string in the first place.When do I use `and` vs `&`?
`and` (or `&&`) is the logical operator — it works on booleans and returns a boolean. `&` is bitwise — it works on the 1s and 0s inside a number. You almost always want the logical one when writing conditions.