Expressions and operators.
Category : Scala
| Sub Category : Scala Programs | By Prasad Bonam Last updated: 2023-10-20 14:24:28
Viewed : 522
In Scala, expressions are combinations of values, variables, operators, and functions that are evaluated to produce a result. Scala supports various operators for performing arithmetic, logical, and bitwise operations. Here is an overview of some common operators and expressions in Scala:
Arithmetic Operators:
- + (Addition): Adds two operands.
- - (Subtraction): Subtracts the second operand from the first.
- * (Multiplication): Multiplies two operands.
- / (Division): Divides the first operand by the second.
- % (Modulus): Returns the remainder of the division of the first operand by the second.
Comparison Operators:
- == (Equality): Checks if two operands are equal.
- != (Inequality): Checks if two operands are not equal.
- > (Greater than): Checks if the first operand is greater than the second.
- < (Less than): Checks if the first operand is less than the second.
- >= (Greater than or equal to): Checks if the first operand is greater than or equal to the second.
- <= (Less than or equal to): Checks if the first operand is less than or equal to the second.
Logical Operators:
- && (Logical AND): Returns true if both operands are true.
- || (Logical OR): Returns true if at least one of the operands is true.
- ! (Logical NOT): Returns true if the operand is false and vice versa.
Bitwise Operators:
- & (Bitwise AND): Performs the AND operation on each bit of the operands.
- | (Bitwise OR): Performs the OR operation on each bit of the operands.
- ^ (Bitwise XOR): Performs the XOR operation on each bit of the operands.
- << (Left shift): Shifts the bits of the first operand to the left by the number of positions specified by the second operand.
- >> (Sign-propagating right shift): Shifts the bits of the first operand to the right by the number of positions specified by the second operand.
These operators can be used with variables and values to create various expressions that perform different operations. Scala supports complex expressions, allowing you to combine multiple operators and values to create intricate logic and computations. Understanding these expressions and operators is essential for writing effective and concise Scala code.