Expressions perform specific actions, based on an operator, with one or two operands. An operand can be a constant, a variable or a function result. Operators are arithmetic, logical, and relational. As with C, some operators vary in functionality according to the data type of the operands specified in the expression.
The negative unary operator reverses the sign of an expression from positive to negative or vice-versa. The net effect is that of multiplying the number by -1. Example:
a = -10;
The Prefix operators increment or decrement the variable prior to dereferencing the object, while the Postfix operators increment or decrement the variable after referencing it. Example:
A=1; B = a++; // b will equal 1, a will equal 2; A = 1; B = ++a; // b will equal 2, a will equal 2; A= 1; B = a--; // b will equal 1, a will equal 0;
Symbol | Operation | Example | Description |
---|---|---|---|
+ | Addition | a + b | Add the two operands |
- | Subtraction | a - b | Subtract the second operand from the first operand |
* | Multiplication | a * b | Multiply the two operands |
/ | Division | a / b | Divide the first operand by the second operand |
** | Power | a ** b | Raise the first operand by the power of the second operand |
% | Modulo |
a % b |
Divide the first operand by the second operand and yield the remainder portion |
If an expression contains two or more operators with the same precedence, the operator to the left is evaluated first. For example, 10 / 2 * 5 will be evaluated as (10 / 2) and the result multiplied by 5.
When a lower precedence operation should be processed first, it should be enclosed within parentheses. For example, 30 / 2 + 8. This is normally evaluated as 30 divided by 2 then 8 added to the result. If you want to divide by 2 + 8, it should be written as 30 / (2 + 8).
Parentheses can be nested within expressions. Innermost parenthetical expressions are evaluated first.
Symbol | Operation | Example | Description |
---|---|---|---|
AND && | AND |
Expr1 $$ expr2 |
True if both expr1 and expr2 are true. |
OR || | OR |
Expr1 OR expr2 |
True if either expr1 or expr2 are true. |
Symbol | Operation | Example | Description |
---|---|---|---|
< | Less than | a < b | True if a is less than b. |
> | Greater than | a GT b | True if a is greater than b. |
== | Equal | a == b | True if a is equal to b. |
!= | Not equal | a NE b | True if a is not equal to b. |
<= | Less than or equal | a <= b | True if a is less than or equal to b. |
>= | Greater than or equal | a GE b | True if a is greater than or equal to b. |
Comparisons must be made on like data types—string to string, numbers to numbers. If the data types are different, a run time error will be raised.
If any character in a string does not match the corresponding character in the other string, the comparison stops and the strings are considered not equal. Strings are case-sensitive. Examples:
Str1 = "abcdefg"; Str2 = "abcdefg"; Str3 = "abcdef"; Str4 = "ABCDEFG"; If (str1 == str2)... results in an equal/true conditions If (str1 == str3).... Results in a not equal condition because str3 is shorter than str1 If (str1 == str4) ... Results in a not equal condition because the strings are of different case;
Str1 = "abc"; Str2 = "def"; Str3 = str1 + str2; results in "abcdef"