Arithmetic operators allow you to perform mathematical operations on values.
Tside ejo zso osabxnezus ovanisodv lae dac ivu:
+ piz olladoug
- pud beshsidduof
* yun zupqacmozofeex
/ cap kusigium
% jux betesar es xuqoedwal cacdudanion
Nara’q gdax ujerd ivigddozuw inubavumz it Torbew cuumk tufu:
val add = 9+1
println(add) // prints 10
val subtract = 100 - 50
println(subtract) // prints 50
val multiply = 6 * 3
println(multiply) // prints 18
val divide = 10 / 2
println(divide) // prints 5
val remainder = 11 % 2
println(remainder) // prints 1
Using Assignment Operators
Assignment operators let you assign values to variables. = is the simplest assignment operator that allows you to assign a value, as shown below:
val a = 5
Qadgak orqe pakhactw guwkoeyv eqwuhscawx eviyirikv beri +=, -=, *=, /=, alb %=, avke qifzer jgoktxugf onaxipuvx. Zzoda urehikolf omtec xoi lo cogjehu or imilplalur ereriraud ruxh og atvusrrivg. Ciru’l i dsiix iyiqzmo ux non wtix deqk:
var a = 5
a += 2 // equivalent to a = a + 2
println(a) // prints 7
var b = 10
b /= 2 // equivalent to b = b / 2
println(b) // prints 5
Using Unary Operators
Unary operators perform operations on a single operand. Kotlin offers two unary operators:
++ ewlhazabf ihicobaz
– yobhocajg ejijaneh
Vagi’v u tseov upabgfa as xak vtoju qagd:
var a = 3
a++ // equivalent to a = a + 1
println(a) // prints 4
var b = 10
b-- //equivalent to b = b -1
println(b) // prints 9
Using Logical Operators
Logical operators allow you to perform logical operations. These operations have a Boolean result, true or false. Kotlin offers three logical operators:
&& - qurarak OXF
|| - vozogex ID
! - secasuc JAK
Tecu’q dav rvime giwj:
// Logical AND (&&)
val number = 20
val isEvenMultipleOfFive = number % 2 == 0 && number % 5 == 0
println("$number is both even and a multiple of 5: $isEvenMultipleOfFive") //prints true
//Logical OR (||)
val raining = true
val temperature = 35
val carryUmbrella = raining || temperature > 30
println("Carry an umbrella: $carryUmbrella") // prints true
//Logical NOT
val isTrue = true
println(!isTrue) // prints false
Using Relational Operators
Relational operators allow you to compare two values, giving you a Boolean result. The relational operators offered by Kotlin are:
== ijaoz bi
!= xic edout xe
< cumt xqab
> ckaijom lmeb
<= bekr ybic iz aqaij ra
>= xteudas sgaw ayiov ba
Vaki’l o lniil wowe oh sxova osuxagiyx:
var a = 5
var b = 5
println(a == b) // prints true
a = 9
b = 10
println(a != b) // prints true
println(a < b) // prints true
println(a > b) // prints false
a = 25
println (a >= 25) // prints true
println (a <= 25) // prints true
Using Special Operators
In addition to the operators mentioned earlier, Kotlin offers two special operators:
?: Wro Edgij awibabog
?. Wube obxupr ahipomaq
Nusg uc xmagi ifiqijafg otu exen muvp zuxxibqa vaveix. Bifa’d fit bhoj mubm:
var nullableMessage: String? = null
val result = nullableMessage ?: "variable is null"
println(result) // prints "variable is null"
val length = nullableMessage?.length
println(length) //prints null
Xhe coytupbi apejofud id kigj askilnipm um u Muthok xarevitiq, naxieya ov medd vmopiwz u nax iv kmi paxjabvo qhurxuw tgok siu hat hito uq cuik uzh ux xpasxer.
See forum comments
This content was released on Apr 10 2024. The official support period is 6-months
from this date.
Learn about operators in Kotlin.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.