OPERATORS–Operators is the term used to describe the action to be taken between one or more data expression. An Identifier used with an operator is called an operand.
Some Operators operate on a single operand and are called operators. The other operators indicated before operands and are called prefix operators. The other operators indicated after operands are called postfix operators. Most operators are embedded between two operands are called ternary operator.
VARIOUS OPERATORS IN C++ IS DESCRIBED BELOW
1.ARTHIMETIC OPERATORS–These operators are used to do arithmetic. C++ provides for five basic arithmetic calculation: Addition, Subtraction, Multiplication, Division, and Reminder.
C++provides following arithmetic operators:
2.ASSIGNMENT OPERATORS–An assignment operator is used to assign the value to a variable.
3.UNARY OPERATORS OR INCREMENT?DECREMENT OPERATORS–Increment(++)and decrement(–) operators are called unary operators because they operate on single variable only. The Increment operator(++) increment the value by 1.
++a –> a=a+1;
The decrement operator(–) decrese the value by 1.
–a –> a=a+1;
These opearators are of two types–
(i)Prefix increment/decrement– The value incremented/decremented, then the operation is performed.
(ii)Postfix increment/decrement–First the operation is performed and then the value is incremented/decrement.
4.RELATIONAL OPERATORS–The relational and equality operators are used to test or compare the value between two operands i.e., between two variables or between a variable constant. If the condition is false, then the integer is 0. if the condition is true, the integer is non-zero. The logical operators also produce the same result, 0 for false and a non-zero for true.
5.LOGICAL OPERATORS–The logical operators are called Boolean operators because the test between values are reduced to either true or false, with zero being false and one being true.
The logical operators are as follows-
(i) The AND(&&) operates gives the result true. If both (all) the conditions have the true value, otherwise gives the result false.
(ii)OR(||)operator gives the result true areIf any one of the conditions or both the Condition is true.
(iii)NOT(!) operator gives the result true,, I f the condition is false and If the condition is true.
6.CONDITIONAL OPERATOR–The conditional operator(?) is a condensed form of an if-then-else statement. The Question mark and colon are a special character used to form the conditional expression operator.
Syntax Expression 1? Expression 2: Expression 3
Expression 1 is evaluated first. If it is true, then Expression 2 is evaluated or fix it is false, then Expression 3 is evaluated.
(i)SIZE OF OPERATOR– The size fo operator return the number of bytes required to represent a data type of variable.
size of (data type)
The data type can be standard or user defined.
e.g. size of (int)
returns value 2
(ii)COMMA (.) OPERATOR– A set of expression is separated by commas in C++. it links related expression together. Expression linked using comma operator are evaluated from left to right and the value of the right most expression is the result.
The expression is evaluated from left to right. First, b is assigned a value of 5, then expression b+6 is evaluated giving 11. Hence, a=11.
PROCEDURE AND ASSOCIATIVITY
All C++ operators have two attributes. An understanding of both of these is a prerequisite for their proper use. These two attributes are known as precedence and associativity. Precedence refers to the rank or urgency of an operator.
For example, The equation a=6+4%2 would simplify to a=8. because the division operation has precedence over the addition and equality operation.
Associativity refers to an order (direction) in which operators possessing the same precedence are to be executed. Again, utilizing an algebraic tutorial. the equation y=6%3*5%2 would simplify to y=5 because the multiplication and division operators have equal precedence and are associated left to right. An equivalent expression would be y=((6 % 3) *5)%2. If the associativity of these operators had been from right to left, the original equation would have simplified to y=8.
An expression may consist of one or more operands and zero or more operators produce a value Expressions are of four types.
(i)CONSTANT EXPRESSION– It consists only of constant values.
e.g. 22, 23.6, + 4.6/3.6.
(ii) INTEGRAL EXPRESSION– These are those which produce integer result after implementing all automatic and explicit type conversions.
e.g. X*Y -11, (X +Y)-Z *. 45 + X + int (2.5).
(iii)FLOAT EXPRESSION –These are those which after all conversion produce floating point results e.g.(X+Y) /37. 37+float (16)/11.
(iv) POINTER EXPRESSION– It produces a memory address values.
e.g. &X, ptr, “abc” etc.
In certain situations, some variables are declared as integers but sometimes it may be required to get the result as floating point numbers. The type conversion is to convert the set declared type to another required type. it is easy to convert a value from one type to another type in a C++ program.
Type revision is of two forms–
(i)IMPLICIT– It is performed automatically by the compiler without programmer intervention.
(ii)C++ allows the programmer to create a variable and to change its type by using the type cast operators and process is called casting.