Add, compare, repeat and split values with operators — and watch precedence pick the order for you.
The big picture
The symbol toolbox
Operators are the punctuation of math and logic — a handful of symbols that compute, compare and combine values.
1
Math first
+ - * / and % do arithmetic; ** raises to a power: 2 ** 3 is 8.
2
Division gotcha
Two Integers divided stay Integer: 7 / 2 is 3, and % grabs the leftover 1.
3
Comparisons
==, !=, < and > return true or false — perfect for feeding an if.
4
Logic glue
&& means both, || means either, and ! flips the answer.
The symbol toolbox
Math first1+ - * / and % do arithmetic; ** raises to a power: 2 ** 3 is 8.
Division gotcha2Two Integers divided stay Integer: 7 / 2 is 3, and % grabs the leftover 1.
Comparisons3==, !=, < and > return true or false — perfect for feeding an if.
Logic glue4&& means both, || means either, and ! flips the answer.
4 building blocks make up The symbol toolbox
How it works
Precedence in motion
Watch 2 + 3 * 4 get evaluated — multiplication jumps the queue before addition ever runs.
Watch it run
Operators at work
Arithmetic first, then comparisons — every line prints one true answer.
math.rb
1a =10
2b =3
3
4puts a + b
5puts a / b
6puts a % b
7
8puts a > b
9puts a == b
10puts a != b
main
a gets 10.
Memory
a= 10
Console
output appears as the program runs…
Step 1/8
Practice
Match the operators
Click an operator, then click what it does to lock the pair.
6 conceptsTap a card to expand
?+drops into place
7/2=3
7%2=1
2**3=8
?==drops into place
?&& / ||drops into place
Challenge
Predict the output
Integer division and one sneaky Float — trace each line before you guess.
divide.rb
1x =7
2y =2
3
4puts x / y
5puts x % y
6puts x /2.0
7puts(x + y)*2
Your predicted output — one line per console line
Hint: Two Integers divided give an Integer — the part after the decimal point vanishes. One Float anywhere makes the result a Float, and parentheses win over every operator.
Recap
Key takeaways
Key takeaways5 takeaways
01
7 / 2 is 3 with Integers — use 7 / 2.0 for the decimal answer.
02
% is the remainder operator: 7 % 2 is 1.
03
* and/ bind tighter than+ and- — use parentheses to be sure.
04
Comparisons return true or false, ready for an if.