Welcome to a new chapter of our Kotlin journey: arithmetic and logical operations. These operations are essential in programming for performing calculations and facilitating decision-making. Let's start exploring!
Arithmetic operations in Kotlin include addition (+
), subtraction (-
), multiplication (*
), division (/
), and modulus (%
). The modulus operation, %
, calculates the remainder after dividing one number by another. For instance, 7 % 3
equals 1
because 7 divided by 3 leaves a remainder of 1. We'll explore these operations through examples using variables a
and b
:
Kotlin1val a = 10 2var b = 2 3 4println(a + b) // prints: 12 5println(a - b) // prints: 8 6println(a * b) // prints: 20 7println(a / b) // prints: 5 8println(a % b) // prints 0, because the remainder of 10/5 is 0
In Kotlin, increment (++
) and decrement (--
) operators play a pivotal role in controlling the flow of loops and modifying variable values with precision. These operators either increase or decrease the value of a variable by one. They are available in two forms: prefix and postfix.
- Prefix Increment/Decrement: When used before a variable (e.g.,
++count
or--count
), the operation is performed, and the new value of the variable is used in the expression immediately. - Postfix Increment/Decrement: When used after a variable (e.g.,
count++
orcount--
), the current value of the variable is used in the expression, and then the operation is performed.
Kotlin1var level = 1 2println(++level) // Outputs 2: level is incremented before printing. 3println(level--) // Outputs 2: level is printed, then decremented. 4println(level) // Outputs 1: reflects the decrement.
Kotlin simplifies arithmetic operations combined with assignment through compound assignment operators. These operators, including +=
, -=
, *=
, /=
, and %=
, merge an arithmetic operation with an assignment, streamlining code and enhancing readability.
Kotlin1var score = 100 2score += 10 // same as `score = score + 10`, score is now 110 3score -= 20 // same as `score = score - 20`, score is now 90 4score *= 2 // same as `score = score * 2`, score is now 180 5score /= 3 // same as `score = score / 3`, score is now 60 6score %= 7 // same as `score = score % 7`, score is now 4
Logical operations, specifically AND (&&
), OR (||
), and NOT (!
), within Kotlin are indispensable for making branching decisions. AND (&&
) returns true
only if both operands are true. OR (||
) returns true
if either operand is true. NOT (!
) inverts the boolean value. Consider an example of an in-game task:
Kotlin1val hasEnoughPoints = true 2val hasEnoughCoins = false 3 4println(hasEnoughPoints && hasEnoughCoins) // prints: false 5println(hasEnoughPoints || hasEnoughCoins) // prints: true 6println(!hasEnoughCoins) // prints: true
Arithmetic and logical operations can be combined. Let's express the conditions for a game through such combinations:
Kotlin1val points = 150 2val coins = 5 3val isPremiumMember = true 4 5val canUnlockLevel = points > 100 && (coins > 10 || isPremiumMember) 6println(canUnlockLevel) // prints: true
Congratulations on becoming familiar with arithmetic and logical operations in Kotlin! You've mastered how these operations can shape our programs and influence behavior based on specific conditions. We'll reinforce this knowledge with some practice exercises. Are you ready? Let's dive in!