Operators in Kotlin
Math Operators
Kotlin provides a set of math operators that you can use to perform basic arithmetic operations. Here are some of the commonly used math operators in Kotlin:
-
Addition: The
+
operator is used to add two numbers together. For example,val sum = 5 + 3
will assign the value8
to the variablesum
. -
Subtraction: The
-
operator is used to subtract one number from another. For example,val difference = 10 - 5
will assign the value5
to the variabledifference
. -
Multiplication: The
*
operator is used to multiply two numbers. For example,val product = 2 * 4
will assign the value8
to the variableproduct
. -
Division: The
/
operator is used to divide one number by another. For example,val quotient = 10 / 2
will assign the value5
to the variablequotient
. -
Modulus: The
%
operator is used to find the remainder of a division operation. For example,val remainder = 10 % 3
will assign the value1
to the variableremainder
. -
Increment and Decrement: The
++
and--
operators are used to increment and decrement a number by1
respectively. For example,var count = 0; count++
will increment the value ofcount
by1
. -
Compound Assignment: Kotlin also provides compound assignment operators that allow you to perform an operation and assign the result to the same variable in a single step. Here are some examples:
- Addition Assignment: The
+=
operator is used to add a value to a variable and assign the result back to the same variable. For example,var num = 5; num += 3
will add3
to the value ofnum
and assign the result8
back tonum
. - Subtraction Assignment: The
-=
operator is used to subtract a value from a variable and assign the result back to the same variable. For example,var num = 10; num -= 5
will subtract5
from the value ofnum
and assign the result5
back tonum
. - Multiplication Assignment: The
*=
operator is used to multiply a variable by a value and assign the result back to the same variable. For example,var num = 2; num *= 4
will multiply2
by4
and assign the result8
back tonum
. - Division Assignment: The
/=
operator is used to divide a variable by a value and assign the result back to the same variable. For example,var num = 10; num /= 2
will divide10
by2
and assign the result5
back tonum
.
- Addition Assignment: The
These are just a few examples of the math operators available in Kotlin. You can use them to perform various calculations and manipulate numeric values in your code.
Boolean Operators
Boolean operators in Kotlin are used to perform logical operations on boolean values. These operators allow you to combine or manipulate boolean expressions to make decisions or control the flow of your program. There are three main boolean operators in Kotlin:
-
Logical AND (
&&
): The logical AND operator returnstrue
if both operands aretrue
, andfalse
otherwise. For example,val result = (x > 5) && (y < 10)
will assigntrue
to result if both conditions(x > 5)
and(y < 10)
aretrue
. -
Logical OR (
||
): The logical OR operator returnstrue
if at least one of the operands istrue
, andfalse
otherwise. For example,val result = (x > 5) || (y < 10)
will assigntrue
to result if either condition(x > 5)
or(y < 10)
istrue
. -
Logical NOT (
!
): The logical NOT operator negates the boolean value of its operand. It returnstrue
if the operand isfalse
, andfalse
if the operand istrue
. For example,val result = !(x > 5)
will assigntrue
to result if(x > 5)
isfalse
.
These boolean operators are essential for making decisions and controlling the flow of your program based on certain conditions. They allow you to create complex boolean expressions by combining multiple conditions together.