You are here
Home > Kotlin

Kotlin:Operators

kotlin Operators

Kotlin Operators

In this post we will discuss about kotlin operators in details with some examples. Kotlin support arithmetic, assignment, comparison, increment and decrement operators.

Arithmetic operators

  • Kotlin also support all arithmetic operators that other languages support.

Operator

Meaning
+

Add numbers(also used for concatenation)

Subtract number
*

Multiply number

/

Divide number
%

Return Remainder

Example:
fun main(args:Array<String>)
{
print(“enter n1:”);
var n1:Float= readLine()!!.toFloat();
print(“enter n2:”);
var n2:Float= readLine()!!.toFloat();
var sum:Float?;
sum=n1+n2;
println(“sum=”+sum);
var sub:Float?;
sub=n1-n2;
println(“subtraction=”+sub);
var mult:Float?;
mult=n1*n2;
println(“multiplication=”+mult);
var div:Float?;
div=n1/n2;
println(“Division=”+div);
var mod:Float?;
mod=n1%n2;
println(“Modulus=”+mod);
}
output:

Explanation:
Here we accept two numbers of Float type from user using the readLine() function. We perform the addition, subtraction, multiplication, division, modulus.

Assignment Operators

  • Assignment operators are used to assign value to variable.

Expression

Equivalent to
x+=1

x=x+1

x-=1

x=x-1
x*=1

x=x*1

x/=1

x=x/1
x%=1

x=x%1

Example:
fun main(args: Array<String>)
{
var num1:Int=10;
num1+=10;
println(“Assignment + in num1=”+num1);
var num2:Int=20;
num2-=5;
println(“Assignment – in num2=”+num2);
var num3:Int=5;
num3*=2;
println(“Assignment * in num3=”+num3);
var num4:Int=10;
num4/=2;
println(“Assignment / in num4=”+num4);
}
output:

Explanation:
Here we create four variables of Integer type and we perform different assignment operation on that variables.

Comparison Operators

  • Comparison operators are used in control structure like if statement, when expression, loops etc.

Operator

Meaning Expression
< Less than

x<y

>

Greater than x>y
<= Less than or equal to

x<=y

>=

Greater than or equal to x>=y
== Is equal to

x==y

!=

Is not equal to

x!=y

Example:
fun main(args:Array<String>)
{
var num1=10;
var num2=20;
if(num1<num2)
{
println(“num1 is less than num2=”+num1);
}
else
{
println(“num2 is less than num1=”+num2);
}
if(num1>num2)
{
println(“num1 is Greater than num2=”+num1);
}
else
{
println(“num2 is Greater than num1=”+num2);
}
if(num1==num2)
{
println(“num2 is equal to num1”);
}
}
output:

Explanation:
Here we create a two variables num1 and num2 and perform comparison operation.

Increment and Decrement Operators

  • Increment and decrement operators are unary operators means it operates on single variable.
  • Increment /decrement operator is used to increment or decrement a value store inside a variable.
  • By default, increment/ decrement operator increment or decrement value by 1.
  • These operators can be prefix or postfix. If we use prefix increment than it first increment value of variable and then assign the value to the variable.

Operator

Meaning
a=++b

First b is incremented by one and then assign to a.

a=b++

First b is assign to a and then b is incremented by one.

a=–b

First b is decremented by one and then assign to a.

a=b–

First b is assign to a and then b is decremented by one.

Example:
fun main(args:Array<String>) {
var n1=10;
var n2=11;
var n3=4;
var n4=5;
var preinc=++n1;
println(“n1 preincrement=”+preinc);
var postinc=n2++;
println(“n2 postincrement=”+postinc);
var predec =–n3;
println(“n3 predecrement=”+predec);
var postdec =n4–;
println(“n4 postdecrement=”+postdec);
}
output:

Keep Visiting TechwithR.com for more upcoming top technical articles and also see my new blog Mag4info. and for kotlin tutorial also see my blog kotlin category.

You can also by SQL 70-761 book write by me from Amazon.
You can also by Kotlin Book write by me from Amazon.

Leave a Reply

Top