Dec 3 Q3: Data Operators
December 2, 2007 For each of the following, describe its function, give an example of one and explain how it might be used in a program.
- Relational operators
- Logical operators
- Arithmetic operators
Relational operator: numerical qualities are one example as they link the relation between 2 entries or values. Some examples are equalities (eg. 5 = 5) and inequalities (eg 4 ≥ 2). The operators predominantly return as true or false, this value relies mainly on the condition that has been set out or not. Relational operator is not limited to mathematics and is also used on computing. Instead of numerical values, they use programming language. Some examples of the use of relational operators are in relating one thing to another (substitution eg x + y = c; print c)
if (x < y) {
printf("x is less than y in this example\n");
Some examples of the uses include of 6 different tests:
- Testing the equivalence of 2 values ( exp1 = exp2 )
- To test the expression on the left is greater than or equal to the expression on the left ( exp1 >= exp2 )
- To test if the expression on the left is less than the expression on the right
( exp1 < exp2 )
- To test if the expression on the right is greater than the expression on the right (exp1 > exp2 )
- To test if the expression on the right is less than or equal to the expression on the left ( exp1 <= exp2 )
- To test if the expression on the left is not equal to the expression on the right ( exp1 != exp2 )
As stated before, the final answer can only be true of false.
Logical operators: Logical operators work to test the falsehood of a pair of expression and if both expression are found correct, they evaluates to 1. Logical expressions are used alongside an “if” statement. But if either statements do not evaluate or are invalid, then the command is bypassed and results in 0. But if this is used incorrectly, then the program could be a source of bugs. The logical negation operator only takes one operand. An operand is “In computer programming, an operand is a term used to describe any object that is capable of being manipulated.” – www.computerhope.com/jargon/o/operand.htm
The operand works from left to right. Some expressions are:
exp1 && exp2 Logical AND
exp1 || exp2 Logical OR
!exp1 Logical NOT
Arithmetic Operators: Arithmetic operators run simply on the basics of to add, to subtract, to divide, to negate or to simply find a value. The expression is written in numeric values and thus the answer is written in numeric value. The basics of arithmetic operations are:
+ - This is a unary arithmetic symbol (unary: denotes if the value of the integer is positive or negative.) it is also to add or subtract two values and also serve binary purposes.
* / These symbols divide or multiply two values, these serve binary purposes.