Expressions in C++
Overview
Expression is the combination of the constants, variables, and operators, which are arranged according to the syntax of C++ language and, after computation, return some values that may be a boolean, an integer, or any other data type of C++. An expression can be a combination of other expressions; while computation, first inner expressions are calculated, then the overall expression. A function call that returns some value could be part of an expression.
Examples of C++ Expression
There are many types of C++ expressions. Let's see some examples. Then further, we will see them in detail.
Constant Expressions
Consist of numeric or fixed values only.
Integral Expressions
Returns integral value after computation.
Float Expressions
Returns float value after computation.
Pointer Expressions
Returns the address of any defined variable of code.
Relational Expressions
Defines the relation between two numerics or arithmetic values.
Logical Expressions
Return logical relation of two relational expressions or arithmetic values.
Bitwise Expression
Applies the operation on the bit level.
Special Assignment Expressions
Assigns the values in the chain
Types of Expressions in C++
Types of expression in C++ are given below:
Constant expressions
Constant expressions are expressions that are made up of only constant values(values that are fixed, like numeric values). This expression value is either fixed means consisting of only one numeric value/constant value or determined at compile time means consisting of some numeric values and operators. Still, these expressions are judged or evaluated at run time only.
We can also use the extern and static keywords with constant values in a constant expression.
Usages of Constant Expressions:
- To define the fixed size of an array at the time of declaration of the size of an array.
- In switch statements as selector case statements.
- While defining bit field size.
Example
Expression Containing Constant | Constant Value |
---|---|
x = 5 + 4; | 5 + 4 |
extern int x = 55 | 55 |
int a = 4 | 4 |
static int c = 3 | 3 |
Let's see the constant expression in the code:
Output
In the above code, first, we declare a variable a. After that, we assign simple constant expressions to it, then simplify printed the value of variable 'a'. Learn more about Constants in C++.
Integral Expressions
Integral expressions are those expressions that produce integer value in the result after executing all the automatic/ implicit (when operands have different data types, the compiler automatically converts smaller data type into a larger data type) and by user/explicit (when user converting a data type into another data type by using (type) operator) type conversion in expression.
Example
Integral Expression | Integer Value |
---|---|
x = (5 * 4) + c; here c is integer. | 20 + c |
int x = (int) 5.0 | 5 |
Let's see the Integral expression in the code:
Output
In the above code, first, we declared variables a, b, and c. Then we take input values of a and b from the user. After that, we assign (int)a+b (an integral expression) to c. It is an explicit conversion. Then we print the value of c.
To explain implicit conversion in integral expression further, we declare two variables one is x, integer type, and another is y, char type. Then we take input, the value of y from the user, and assign it to the x. It is an implicit conversion in which the compiler recognizes by itself that x is an integer type, and we assign a char value to x so it gives the decimal value of char to the x. Then we print the value of x.
Float Expressions
Float expressions produce float values in the result after executing all the automatic/ implicit and user/explicit type conversions in the expression.
Example
Float Expression | float Value |
---|---|
3.54 | 3.54 |
float x = 5.0 | 5.0 |
Let's see the float expression in the code:
Output
In the above code, first, we declared variables a, b, and c. ‘a’ and ‘c’ variables are float type, and the b variable is an integer type value. After that, we assign the a+(float)b value (a floating expression) to c. It is an explicit conversion. Here, we use the '(float)' type operator to convert the overall value in float. Then we print the value of c.
To further explain implicit conversion in floating expression, we declare two variables: ' x, float type, and y, char type. After that, we add yto thex. It is an implicit conversion in which the compiler recognizes by itself that xis a float type, and we added a char value toxso it gives the decimal value of char to thex in a result, we get a floating expression as 'x+y'. Then we print the value of x`.
Pointer Expressions
Pointer expressions produce address value as a result which may be in the hexadecimal form most of the time.
Example
Pointer Expression |
---|
&a |
Ptr (Ptr is pointer) |
Ptr++ |
Let's see the Pointer expression in the code:
Output
In the above code, we first declared an array 'arr' of size 5 and a pointer. After that, assign the base address of the array(a pointer expression) to the pointer ptr. Then increment pointer ptr by 3; print the pointer's value. Now pointer represents the fourth value of the array.
Relational Expressions
Relational expressions are those expressions that produce a bool value in the result. A bool value means the answer is either true or false. Due to this, relational expressions are also known as boolean expressions. Relational expressions are like this: (arithmetic expression) (operator) (arithmetic expression). Here the result of this expression is the relation between both arithmetic expressions. Also, when an arithmetic expression is used on both sides of the relation expression, the first arithmetic expressions are solved, and their result is compared.
Example
Relational Expression | Relation value/boolean value |
---|---|
x = 10 > 5 | True |
y = 5 - 2 < 0 + 1 | False |
Let's see the Relational expression in the code:
Output
In the above code, first, we declare three variables; two of them 'a', and 'b' are integer types, and one 'x' is a boolean type. Assigned value to a and b and assigned a relational expression b>a to x. Then print the value of x. Then we assign another relational expression to x. Here the values of arithmetic expressions are computed by the compiler, then compares both sides and assigns the result to x. After that, print the new value of x.
Logical Expressions
Logical expressions combine two or more relational expressions with the help of logical operators to produce a bool value. Some examples of logical operators are:-
- OR "||" operator
- AND "&&" operator
In the 'OR' operator, if any condition is true, the whole expression's result is true, and if all conditions are false, the result is false. In the 'and' operator, if any condition is false, the whole expression's result is false, and if all conditions are true, then the result is true.
Example
Logical Expression | Logical value/boolean value |
---|---|
x = (10 > 5) || (3 < 2) | True |
y = (5 - 2 < 0 + 1) && (2 < 1) | False |
Let's see the Logical expression in the code:
Output
In the above code, first, we declare five variables. Four of them 'a', 'b', 'x' and 'y' are integer types and one 'z' is a boolean type. Assigned value to variables and then assigned a logical expression (b > a)||(x>y) using OR operator to z. Again do the same thing using the "and" operator.
Bitwise Expressions
The expressions in which we apply some operations on bits or bit level. It is used to shift the bits of variables on either left or right side. Some examples of bitwise operators are:-
- Left shift '<<' : The left shift is similar to multiplying by 2. In this, we add an unset bit(0) to the right side of our number.
- Right shift '>>' The right shift is similar to dividing by 2, which means we remove the rightmost bit in this.
Example
Bitwise Expression |
---|
x >> 1 |
x << 3 |
Let's see the Bitwise expression in the code:
Output
In the above code, first, we declared two variables, x and y are integer types and assigned value 4 to both variables. Then applied, the right shift operator on x by 1 bit and the left shift operator on y by 1 bit. After that, print the result. Here x >> 1 and y << 1 are bitwise expressions.
Special Assignment Expressions
In special assignment expressions, we have three types. They are-
- Chained Assignment Expression
- Embedded Assignment Expression
- Compound Assignment Expression
Chained Assignment
Chained Assignment expressions are those expressions in which we assign the same value to more than one variable in a single expression or statement.
Example:
Chain Assignment Expression |
---|
x = y = 34 |
(x = y) = 44 |
Let's see the Chain Assignment expression in the code:
Output
In the above code, first, we declare two variables, x and y. Then we assign values to x and y using the Chained assignment expression x = y = 4 here first y gets its value and then x. Print the value of x and y.
Embedded Assignment Expression
Embedded Assignment expressions are those made up of assignment expressions enclosed by another assignment expression.
Example:
Embedded Assignment Expression |
---|
z = x + (y = 34); |
z = (x = 44) + ( y = 4); |
Let's see the Embedded Assignment Expression in code:
Output
In the above code, first, we declared three variables x, y, and z. Then we assign value to the variable 'z' by embedded assignment expression. In the end, print the value of z.
Compound Assignment Expression
Compound Assignment Expressions are those expressions that consist of assignment expressions and binary expressions together.
Example:
Compound Assignment Expression |
---|
z += x + (y = 34); |
z += (x = 44) & ( y = 4); |
Let's see the Compound Assignment Expression in the code:
Output
In the above code, first, we declared three variables x, y, and z. Then we assign value to the variable 'z'. After that, we assign values to x and y by compound assignment expression. After that, print the value of z.
Conclusion
- Expression is the combination of the constants, variables, and operators, which after computation, returns some value.
- Expression can be of many types depending on the operator, variable type, and arrangement.
- One expression can consist of nested expressions also, and while computation, first, the nested ones are computed, then the outer ones.
- There the three special types of expressions for assignment chained, embedded, and compound assignment expressions.