Expressions and operators.

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2023-10-20 14:24:28 Viewed : 255


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:

  1. + (Addition): Adds two operands.
  2. - (Subtraction): Subtracts the second operand from the first.
  3. * (Multiplication): Multiplies two operands.
  4. / (Division): Divides the first operand by the second.
  5. % (Modulus): Returns the remainder of the division of the first operand by the second.

Comparison Operators:

  1. == (Equality): Checks if two operands are equal.
  2. != (Inequality): Checks if two operands are not equal.
  3. > (Greater than): Checks if the first operand is greater than the second.
  4. < (Less than): Checks if the first operand is less than the second.
  5. >= (Greater than or equal to): Checks if the first operand is greater than or equal to the second.
  6. <= (Less than or equal to): Checks if the first operand is less than or equal to the second.

Logical Operators:

  1. && (Logical AND): Returns true if both operands are true.
  2. || (Logical OR): Returns true if at least one of the operands is true.
  3. ! (Logical NOT): Returns true if the operand is false and vice versa.

Bitwise Operators:

  1. & (Bitwise AND): Performs the AND operation on each bit of the operands.
  2. | (Bitwise OR): Performs the OR operation on each bit of the operands.
  3. ^ (Bitwise XOR): Performs the XOR operation on each bit of the operands.
  4. << (Left shift): Shifts the bits of the first operand to the left by the number of positions specified by the second operand.
  5. >> (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.

Search
Related Articles

Leave a Comment: