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 + 3will assign the value8to the variablesum. -
Subtraction: The
-operator is used to subtract one number from another. For example,val difference = 10 - 5will assign the value5to the variabledifference. -
Multiplication: The
*operator is used to multiply two numbers. For example,val product = 2 * 4will assign the value8to the variableproduct. -
Division: The
/operator is used to divide one number by another. For example,val quotient = 10 / 2will assign the value5to the variablequotient. -
Modulus: The
%operator is used to find the remainder of a division operation. For example,val remainder = 10 % 3will assign the value1to the variableremainder. -
Increment and Decrement: The
++and--operators are used to increment and decrement a number by1respectively. For example,var count = 0; count++will increment the value ofcountby1. -
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 += 3will add3to the value ofnumand assign the result8back 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 -= 5will subtract5from the value ofnumand assign the result5back 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 *= 4will multiply2by4and assign the result8back 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 /= 2will divide10by2and assign the result5back 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 returnstrueif both operands aretrue, andfalseotherwise. For example,val result = (x > 5) && (y < 10)will assigntrueto result if both conditions(x > 5)and(y < 10)aretrue. -
Logical OR (
||): The logical OR operator returnstrueif at least one of the operands istrue, andfalseotherwise. For example,val result = (x > 5) || (y < 10)will assigntrueto result if either condition(x > 5)or(y < 10)istrue. -
Logical NOT (
!): The logical NOT operator negates the boolean value of its operand. It returnstrueif the operand isfalse, andfalseif the operand istrue. For example,val result = !(x > 5)will assigntrueto 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.