0
0
Kotlinprogramming~10 mins

Arithmetic operators in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arithmetic operators
Start
Evaluate operands
Apply operator (+, -, *, /, %)
Compute result
Use result in expression or output
End
This flow shows how Kotlin evaluates arithmetic expressions by first evaluating operands, then applying the operator to compute the result.
Execution Sample
Kotlin
fun main() {
  val a = 10
  val b = 3
  val sum = a + b
  val div = a / b
  println("sum = $sum, div = $div")
}
This code adds and divides two numbers, then prints the results.
Execution Table
StepExpressionOperandsOperatorResultExplanation
1a = 10--10Assign 10 to variable a
2b = 3--3Assign 3 to variable b
3sum = a + b10, 3+13Add 10 and 3 to get 13
4div = a / b10, 3/3Integer division of 10 by 3 gives 3
5println--sum = 13, div = 3Print the results
6----Program ends
💡 Program ends after printing the results.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
a-1010101010
b--3333
sum---131313
div----33
Key Moments - 2 Insights
Why does dividing 10 by 3 give 3 instead of 3.333?
Because both a and b are integers, Kotlin performs integer division which discards the decimal part. See step 4 in the execution_table.
What happens if we use + operator with two variables?
The + operator adds the values of the two variables. In step 3, a + b adds 10 and 3 to get 13.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the result of a + b?
A13
B30
C7
DError
💡 Hint
Check the 'Result' column in step 3 of the execution_table.
At which step does the program perform integer division?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look for the division operator '/' in the 'Operator' column.
If variable b was changed to 4, what would be the new result of a / b at step 4?
A2.5
B3
C2
D4
💡 Hint
Integer division truncates decimals; 10 / 4 equals 2 in integer division.
Concept Snapshot
Arithmetic operators in Kotlin:
- + addition
- - subtraction
- * multiplication
- / integer division (if both operands are Int)
- % modulus (remainder)
Operands are evaluated first, then operator applied.
Integer division discards decimals.
Full Transcript
This lesson shows how Kotlin uses arithmetic operators to calculate values. First, variables a and b are assigned 10 and 3. Then sum is calculated by adding a and b, resulting in 13. Next, div is calculated by dividing a by b using integer division, which results in 3 because decimals are discarded. Finally, the results are printed. Key points include understanding integer division and how operators work step-by-step.