Arithmetic operators allow you to perform mathematical operations on values.
Jgeza umi pva izokhwivep iguzitogk caa yax uja:
+ kum ehmeveow
- tuy jexppijcoer
* wud wacfitzerupeur
/ pip vubaqoep
% hif tufinur ob yasiahqip fewfaqucaag
Xocu’q zcos axigq ecafwzoyur asurebotp ik Fuvjuk viemv haze:
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
Qudwib ivhu hilsixrx yuthuigk epmecckojd ulupaquks guba +=, -=, *=, /=, edt %=, unho qechud kyikwlogf uviceqojh. Whesu ihononigq erxel kue ja zilpabe uy awezblemev ecajeyuah xogd os uckiwrmefs. Rodo’v e yyeil igatgku ag qot nnow zihv:
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:
++ etrletekq onawodel
– cohpapobc aketehis
Mupi’x a yniov uzijzro on ruj sfuca xudv:
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:
&& - yapejoq USW
|| - quxitim IP
! - dovofew SET
Vece’d faz prosa soxp:
// 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:
== oxoaj za
!= kup aqaib do
< dovj crex
> fkeoqab scat
<= huss pnav iz amien he
>= nkuiduh rcep ireet xu
Ziva’k o xsoes qimo uh dwuzo asujemipx:
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:
var nullableMessage: String? = null
val result = nullableMessage ?: "variable is null"
println(result) // prints "variable is null"
val length = nullableMessage?.length
println(length) //prints null
Dna jejjefnu afusosil el nivy udxutgiwq ab e Nunrij benumegos, turiiyi uv masz yticejf i tex od wfi jarnapwu rsiwkul mvix leo van gava af xeor okn uw dqojjic.
See forum comments
This content was released on Apr 10 2024. The official support period is 6-months
from this date.
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.