Understanding Operators in Kotlin
Think of operators as tools in your programming toolbox! Just like you use different tools for different jobs (hammer for nails, screwdriver for screws), Kotlin has different operators for different tasks with your data.
๐งฎ Arithmetic Operators - The Math Tools
These are your basic calculator operations - perfect for doing math in your code!
โ Addition (+)
Adds numbers together
val total = 5 + 3 // total = 8
val fullName = "Hello" + " " + "World" // fullName = "Hello World"
โ Subtraction (-)
Subtracts one number from another
val change = 10 - 3 // change = 7
val temperature = 25 - 5 // temperature = 20
โ๏ธ Multiplication (*)
Multiplies numbers together
val area = 5 * 4 // area = 20
val totalPrice = 3 * 2.5 // totalPrice = 7.5
โ Division (/)
Divides one number by another
val average = 10 / 2 // average = 5
val half = 7 / 2.0 // half = 3.5
๐ Modulus (%)
Finds the remainder after division
val remainder = 10 % 3 // remainder = 1 (because 10รท3 = 3 with remainder 1)
val isEven = 8 % 2 // isEven = 0 (even numbers have remainder 0 when divided by 2)
โ๏ธ Comparison Operators - The Decision Makers
These operators compare values and help your program make decisions. They always give you true or false answers!
๐ฐ Equal To (==)
Checks if two values are the same
5 == 5 // true
"hello" == "hi" // false
10 == 5 // false
๐ซ Not Equal To (!=)
Checks if two values are different
5 != 5 // false
"hello" != "hi" // true
10 != 5 // true
๐ Greater Than (>)
Checks if first value is larger
10 > 5 // true
3 > 3 // false
5 > 10 // false
๐ Less Than (<)
Checks if first value is smaller
5 < 10 // true
3 < 3 // false
10 < 5 // false
๐ Greater or Equal (>=)
Checks if first value is larger or equal
10 >= 5 // true
3 >= 3 // true
5 >= 10 // false
๐ Less or Equal (<=)
Checks if first value is smaller or equal
5 <= 10 // true
3 <= 3 // true
10 <= 5 // false
๐ Logical Operators - The Rule Makers
These operators combine multiple conditions to create more complex rules for decision-making.
โ AND (&&)
BOTH conditions must be true
val canDrive = (age >= 16) && (hasLicense == true)
// You can drive ONLY if you're 16+ AND have a license
(5 > 3) && (2 < 4) // true (both are true)
(5 > 3) && (2 > 4) // false (second is false)
๐ OR (||)
AT LEAST ONE condition must be true
val canEnter = (hasTicket == true) || (isVIP == true)
// You can enter if you have a ticket OR are a VIP
(5 > 3) || (2 > 4) // true (first is true)
(5 < 3) || (2 > 4) // false (both are false)
๐ NOT (!)
Reverses the condition
val isClosed = !isOpen
// If isOpen is true, isClosed becomes false
// If isOpen is false, isClosed becomes true
!(5 > 3) // false (because 5>3 is true, then reversed)
!(2 > 4) // true (because 2>4 is false, then reversed)
๐ Assignment Operators - The Value Setters
These operators assign values to variables and can do math at the same time!
๐ฐ Basic Assignment (=)
Puts a value into a variable
val name = "Alice"
var score = 0
val isActive = true
โโก๏ธ Add and Assign (+=)
Adds to the current value
var score = 5
score += 3 // score becomes 8 (5 + 3)
var message = "Hello"
message += " World" // message becomes "Hello World"
โโก๏ธ Subtract and Assign (-=)
Subtracts from the current value
var health = 100
health -= 25 // health becomes 75 (100 - 25)
var money = 50.0
money -= 15.5 // money becomes 34.5
โ๏ธโก๏ธ Multiply and Assign (*=)
Multiplies the current value
var quantity = 3
quantity *= 2 // quantity becomes 6 (3 * 2)
var price = 10.0
price *= 1.1 // price becomes 11.0 (10.0 * 1.1)
โโก๏ธ Divide and Assign (/=)
Divides the current value
var total = 20
total /= 4 // total becomes 5 (20 รท 4)
var amount = 15.0
amount /= 3.0 // amount becomes 5.0
๐งช Let's Practice Together!
Can you guess what these operations will result in?
1. What is the result of 8 + 3 * 2?
14 - Multiplication happens first (3ร2=6), then addition (8+6=14)
2. What is (10 > 5) && (3 != 3)?
false - First part is true (10>5), but second is false (3 is equal to 3), so AND gives false
3. If var count = 7, what is count after count += 2?
9 - count was 7, then we add 2, so it becomes 9
4. What is 15 % 4?
3 - 15 divided by 4 is 3 with remainder 3 (because 4ร3=12, 15-12=3)
๐ Quick Reference Guide
Math Operators
+ - Add numbers
- - Subtract numbers
* - Multiply numbers
/ - Divide numbers
% - Find remainder
Comparison Operators
== - Equal to
!= - Not equal to
> - Greater than
< - Less than
>= - Greater or equal
<= - Less or equal
Logical Operators
&& - AND (both true)
|| - OR (at least one true)
! - NOT (reverse)
Assignment Operators
= - Set value
+= - Add to current
-= - Subtract from current
*= - Multiply current
/= - Divide current
๐ก Remember This!
Order of Operations: Multiplication/Division happen before Addition/Subtraction
Use parentheses to make your intentions clear: (8 + 3) * 2 vs 8 + (3 * 2)
You need to be logged in to participate in this discussion.