C++ Expression and there types
An expression is a combination of operators, constants and variables arranged as per the rules of the c language. it may include function call which return values. An expression may consist of one or more operands, and one or more then one operators to produce a value. Expressions may be of the following seven types.
1. constant expressions.
2. Integral Expressions.
3. Float expressions.
4. pointer Expression.
5. Relation Expression.
6. Logical expression.
7. Bitwise expression
As expression may also use combination of the above expressions. Such expressions known as compound expressions
Constant Expressions
constant expression consist of only constant values Example
Integral Expression
Integral Expressions are those which produce integer result after implementing all the automatic and explicit type conversions
Examples
m * n – 5
m * ‘x’
5+ int(2.0)
|
where m and n are integers variables
Float Expressions
Float Expressions are those which, after all conversions, produce floating-point result.
Example
x+y
x*y/10
5+float(10)
10.75
|
where x and y are float variables
pointer Expressions:
pointer Expressions produce address values Examples
where n is a variable and ptr is a pointer variable
Relation Expressions
Relation Expressions yield result of type bool which takes a value true or false
Example
When Arithmetic expressions are used on either side of a relation operator, they will be evaluated first and then the result compared.
Relational expressions are also known as boolean expression
Logical Expressions:
Logical Expression combine two or more relational expression and produces bool type results
Example
x>y && x==19
x==90 || y==9
|
Bitwise Expressions
The Bitwise Expression are used to manipulate data in bit level. they are generally used for testing or shifting bits for Examples
x<<4
y>>1
shift operators are often used for multiplication and division by power of two.
ANSI C++ has introduced what are termed as operator keywords that can be used as alternative representation for operator symbols, operator keywords are given in next
Special Assignment Expressions:
chained Assignment
x=(y=5);
x=y=10;
|
First 10 is assigned to y and then to x
a chained statement cannot be used to initialized variables at the time of declaration for instance the statement
is illegal. This may be written as
float a=13.78,b=13.78;
|
Embedded Assignment:
x=(y=60)+20;
(y=60) is an assignment expression know as embedded assignment. Here the value 60 is assigned to y and then the result 60+20=80 is assigned to x. This statement is identical to
compound assignment:
like the c, c++ supports a compound statement operator which is a combination of the assignment operator with a binary arithmetic operator. for example , assignment statement examples below
y=y+20;
may be written as
y+=20;
The operator +=is known as compound assignment operator. The general form of the compound assignment operator:
variable opt=variable2;
where opt is binary arithmetic operator. This means that
variable1=variable opt variable2;