![]() ![]() If the operator is placed before the variable name, the increment or decrement operation is performed before any other operations are performed on the variable. These operators can be placed either before or after the variable name. The following examples perform exactly the same tasks as the examples above: x++ // Increment x by 1 Instead of using this approach, however, it is quicker to use the ++ and - operators. These expressions increment and decrement the value of x by 1. Consider the code fragment below: x = x + 1 // Increase value of variable x by 1 ![]() The most frequently used of which are outlined in the following table: Operator Description x += y Add x to y and place result in x x -= y Subtract y from x and place result in x x *= y Multiply x by y and place result in x x /= y Divide x by y and place result in x x %= y Perform Modulo on x and y and place result in xĪnother useful shortcut can be achieved using the Kotlin increment and decrement operators (also referred to as unary operators because they operate on a single operand). Numerous compound assignment operators are available in Kotlin. The above expression performs exactly the same task as x = x + y but saves the programmer some typing. This can be simplified using the addition compound assignment operator: x += y ![]() The above expression adds the value contained in variable x to the value contained in variable y and stores the result in variable x. For example, one might write an expression as follows: x = x + y These are primarily of use when performing an evaluation where the result is to be stored in one of the operands. Kotlin provides a number of operators designed to combine an assignment with a mathematical or logical operation. operator, kind of like how adding inc allows us to use the ++ operator.In an earlier section we looked at the basic assignment operator (=). The rangeTo method will allow us to iterate over our range using the. Since we’re defining our custom range, CustomColor class must implement the rangeTo method. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |