Operator

An operator is a symbol that tells the interpreter to perform specific mathematical or logical functions. Silk supports the following operators:

Arithmetic Operators
Silk supports arithmetic operators as follows, assume A is 10, and B is 20
  • +    Adds two operands.    A + B = 30
  • -    Subtracts second operand from the first.    A - B = -10
  • *    Multiplies both operands.    A * B = 200
  • /    Divides numerator by de-numerator.    B / A = 2
  • %   Modulus Operator and remainder of after an integer division.    B % A = 0
  • ++ Increment operator increases the integer value by one.   A++ = 11
  • --    Decrement operator decreases the integer value by one.    A-- = 9
Note: The Increment/Decrement operator in Silk is not absolutely the same as the operator in C. In Silk, i++ is equal to i=i+1, so it does not support left side Increment/Decrement and Assignment with Increment/Decrement.
--i; ++i; a=i++; //error syntax

The number can be added with string,  number will be converted to string automatically before it is added with string:
a=1;
b="2";
print(a+b);//will print 12


Relational Operators
Silk supports the following relational operators, assume A is 1, and B is 2
  • ==   Checks if the values of two operands are equal or not. If yes, then the condition becomes true.     (A == B)  is false
  • !=    Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.    (A != B) is true
  • >     Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.    (A > B) is false
  • <     Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.   (A < B) is true
  • >=  Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.    (A >= B) is false
  • <=  Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.    (A <= B) is true

Logical Operators
Silk supports the following logical operators, assume A is 1, and B is 0
  • &&  Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.    (A && B) is false
  • ||      Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.    (A || B) is true
  • !       Called Logical NOT Operator. If a condition is true, then Logical NOT operator will make it false.  !(A && B) is true

Assignment Operators
  • =    Simple assignment operator. Assigns values from right side operands to left side operand.   
    C = A + B will assign the value of A + B to C
  • +=  Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. 
    C += A is equivalent to C = C + A
  • -=   Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.   
    C -= A is equivalent to C = C - A
  • *=   Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
    C *= A is equivalent to C = C * A
  • /=    Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. 
    C /= A is equivalent to C = C / A
  • %=    Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. 
    C %= A is equivalent to C = C % A
Operators Precedence
Here is the Silk operators precedence listed from higher precedence to lower:
  • Paren     ()      
  • Postfix     [] .      
  • Not         !    
  • Multiplicative     * / %    
  • Additive     + -    
  • Relational     < <= > >=    
  • Equality      == !=
  • Logical AND     &&      
  • Logical OR     ||    
  • Assignment     = += -= *= /= %= ++ --

Silk currently does not support bitwise operators.