Second Lesson of Algorithm

This time, we continued to learn on C programming language in general. First we learned about operator, operand, and arithmetic. Operator is symbol used to process a value into a new value. For example addition (+) and equal (=). While operand is the data or value which was processed by operator, like numbers and variables.

Based on the number of operand(s), operator is divided into 3. Unary operator is an operator that used one (1) operand. Binary operator is an operator that used two (2) operands. While ternary operator is an operator that used three (3) operands.

Based on the operation type, operator is divided into 6. First, Assignment operator, is used to assign a value/data to a variable. For example, equal (=). X = 10, then the value ten (10) is given to variable X. Second, logical operator, is used on logical expression. For example, not (!), and (&&), or (||), and xor/exclusive or (!=).

Third, Arithmetic operator, is used on arithmetics or counting. For example, addition (+), subtraction (-), multiplication (*), division (/), modulo (%), increment (++), decrement (–), scope ( ), and power (^). Increment and decrement can be divided into pre-increment, pre-decrement, and post-increment, post-decrement. Pre-increment (++x) and pre-decrement (–x) works by adding or subtracting the value of the variable (x) first before continuing the equation/expression. While post-increment (x++) and post-decrement (x–) works by processing the equation/expression first then adding or subtracting the value of the variable (x). Arithmetic operator can also be used as combined operator. For example, “X += 3” have the same meaning as “X = X + 3” same thing with other arithmetic operators.

Fourth, relational operator, is used to compare and to get the value of True or False. For example, equality (==), less than (<), less or equal than (<=), greater than (>=), greater or equal than (>=), not equal (!=), and conditional assignment (?:). In C language, False equals to 0 and True equals to “not 0” but by default, in C, True equals to 1. Conditional assignment is another form of “if-else condition”. For example, X = (A < B) ? A : B. It means that if A < B then X = A else X = B.

Fifth, Bitwise operator, is used on operations using bit. For example, and (&), or (|), xor (^), complement (-), shift left (<<), shift right (>>). All of bitwise operators will do the operations in bit. Pointer operator will be discussed in the next couple of meetings.

All of those operators have precedence and associative. Precedence is the order of the operators that will occur on the execution. Higher-level precedence operators will execute first before the lower-level precedence operators. For example, “X = A + B * C”. “B * C” will be execute first then the result will be executed with “+ A” and finally the equal (=) will be executed. Meanwhile, associative is the order of execution for the same-level precedence operators, either right to left or left to right. For example, “X = A / B * C”. Division (/) and multiplication (*) have the same level of precedence, so we need to see the associative for that precedence level. It turns out that for division and multiplication, the associative is from left to right. So, we execute the division first then the multiplication and finally the equal.

That’s all for this meeting. Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *