Some My Experiences

Header Ads

Sunday 16 December 2018

Operators in Blogger

Arithmetic Operators
Blogger provides arithmetic operator to perform addition (+), subtraction (-), multiplication (*), division (/) and modulo/remainder (%). Additive operator (+) also used for string concatenation.

Operator Description
+ Adds Left-Operand and Right-Operand, also use for concatenate strings
- Subtracts Right-Operand from Left-Operand
* Multiplies values on either side of the operator
/ Divides Left-Operand by Right-Operand
% Computes the remainder of dividing Left-Operand by Right-Operand


Example:
<b:with var='totalComments' value='9'>
    data:totalComments = <b:eval expr='data:totalComments' /><br/>
    data:totalComments + 2 = <b:eval expr='data:totalComments + 2' /><br/>
    data:totalComments - 2 = <b:eval expr='data:totalComments - 2' /><br/>
    data:totalComments * 2 = <b:eval expr='data:totalComments * 2' /><br/>
    data:totalComments / 2 = <b:eval expr='data:totalComments / 2' /><br/>
    data:totalComments % 2 = <b:eval expr='data:totalComments % 2' /><br/>
    "Hello" + " World" = <b:eval expr='"Hello" + " World"' /><br/>
</b:with>

Result:
data:totalComments = 9
data:totalComments + 2 = 11
data:totalComments - 2 = 7
data:totalComments * 2 = 18
data:totalComments / 2 = 4.5
data:totalComments % 2 = 1
"Hello" + " World" = Hello World


Relational Operators
The relational operator determines the relation between two operand. Relational operator compare two operand and determine if one operand is equal, not equal, greater than, less than, greater than or equal, less than or equal to another operand.

Operator Description
== Equal to. True if two operands are equal
!= Not equal to. True if two operands are not equal
> Greater than. True if Left-Operand is greater than Right-Operand
< Less than. True if Left-Operand is less than Right-Operand
>= Greater than or equal to. True if Left-Operand is greater than or equal to Right-Operand
<= Less than or equal to. True if Left-Operand is less than or equal to Right-Operand

Example:
5 == 7 = <b:eval expr='5 == 7' />
5 != 7 = <b:eval expr='5 != 7' />
5 > 7 = <b:eval expr='5 > 7' />
5 < 7 = <b:eval expr='5 < 7' />
5 >= 7 = <b:eval expr='5 >= 7' />
5 <= 7 = <b:eval expr='5 <= 7' />

Result:
5 == 7 = false
5 != 7 = true
5 > 7 = false
5 < 7 = true
5 >= 7 = false
5 <= 7 = true


Conditional or Logical Operator
The logical operators || (OR) and && (AND) operates two boolean expression and return boolean value.
There is logical NOT/complement operator, which take one operand, unary operator category. This operator used to inverses boolean value of its operand.
There are another conditional operator called Ternary Operator. Called ternary operator because it take 3 (Three) operand, and only this operator that take 3 operand. Ternary operator is like simplified b:if, b:else statement, but with return value. Format: operand1 ? operand2 : operand3
Operand1 must be boolean expression, operand2 and operand3 can be any data or expression that will be return value. Ternary operator will return operand2 value if operand1 is true, otherwise return value of operand3.

Operator Description
&&, AND conditional-AND; true if all operand are true.
||, OR conditional-OR; true if either of the operand is true
! logical NOT, return negation of its boolean operand
?: Ternary Operator, return operand2 value if operand1 is true, otherwise return value of operand3.

Example:
9 == 7 AND 9 > 7 returns <b:eval expr='9 == 7 AND 9 > 7' /><br/>
9 == 7 OR 9 > 7 returns <b:eval expr='9 == 7 OR 9 > 7' /><br/>
!(9 == 7) returns <b:eval expr='(9 == 9)' />
9 == 9 ? "Equal" returns "Not equal" : <b:eval expr='9 == 9 ? "Equal" : "Not equal"' /><br/>
Result:
9 == 7 AND 9 > 7 returns false
9 == 7 OR 9 > 7 returns true
!(9 == 7) returns true
9 == 9 ? "Equal" : "Not equal" returns Equal

No comments:

Post a Comment