Updated April 10, 2023

## Introduction to C++ Expression

An expression in C++ is an order collection of operators and operands which specifies a computation. An expression can contain zero or more operators and one or more operands, operands can be constants or variables. In addition, an expression can contain function calls as well which return constant values. The result obtained after evaluation expression is assigned to the variable by using the assignment operator. Consider some of an examples of an expression in C++ : b = 25 + a, a / (b + c), x = 6.75 * 2, x == 2.6. etc.

### Categories of expression in C++ –

There are different categories of expression in C++ based on the result obtained after evaluation of an expression or based on the types of an operand present in an expression.

**1. Constant expressions –** Constant expressions contain only constant values in an expression.

**Example: **

`x=25+10`

**2. Integral expressions –** Integral expressions that result in an integral value after evaluating an expression.

**Example: **

`x + int(12.0)`

**3. Float expressions –** Float expressions that result from float values after evaluating an expression.

**Example : **

`x+float(9)`

**4. Pointer expressions –** pointer expression which results in an address of a variable after evaluating an expression.

**Example : **

`y=&x`

**5. Relational expressions –** Relation expression which results from a bool value either true or false after evaluating an expression.

**Example: **

`a-b >= x-y`

**6. Logical expressions –** Logical expression which results in a bool value either true or false after evaluation a combination of two or more relational expressions.

**Example: **

`a>20 || b==20`

**7. Bitwise expressions –** Bitwise expressions perform the operation at a bit level in an expression.

**Example: **

`x&4.`

### Examples of C++ Expression

Here are the following examples mention below

#### Example #1 – Constant expressions

This expression contains only constant values in an expression. The constant values can be integer, float, character, double, enumeration constants. In this expression value is find at compile-time, evaluate at run time. The constant expression can be used for the index of an array, for numeric value in the enum, for case match in switch case, etc.

Next, we write the C++ code to understand the constant expression more clearly with the following example –

**Code:**

```
#include <iostream>
using namespace std;
int main()
{
// declaration of variable
int result;
//constant expression
result = 3 + 2 * 10;
// display result value
cout<<"The result of constant expression = "<<result;
return 0;
}
```

**Output:**

#### Example #2 – Integral Expressions

This expression results from an integral value after evaluating an expression, If needed an expression performs implicit and explicit conversions.

Next, we write the C++ code to understand the integral expression more clearly with the following example –

**Code:**

```
#include <iostream>
using namespace std;
int main()
{
// declaration of variables
int a=2, b=3, result;
//integral expression
result = a*a + b*b;
// display result value
cout<<"The result of integral expression = "<<result;
return 0;
}
```

**Output:**

#### Example #3 – Float Expressions

This expression results from a floating value after evaluating an expression, If needed a can expression performs implicit and explicit conversions.

Next, we write the C++ code to understand the float expression more clearly with the following example –

**Code:**

```
#include <iostream>
using namespace std;
int main()
{
// declaration of variables
float a=2.5, b=3.6, result;
//float expression
result = (a+b)/float(10);
// display result value
cout<<"The result of float expression = "<<result;
return 0;
}
```

**Output:**

#### Example #4 – Pointer Expressions

This expression result is the address of another variable after evaluating an expression. The pointer expression contains &x, ptr++, ptr–, ptr and so on expression.

Next, we write the C++ code to understand the pointer expression more clearly with the following example –

**Code:**

```
#include <iostream>
using namespace std;
int main()
{
// declaration of variables
int a=10, result;
// declaration of pointer
int *ptr;
// pointer initialize
ptr=&a;
cout<<"The value of a = "<<a<<endl;
cout<<"The ptr pointing to address = "<<ptr<<" which store value = "<<*ptr<<endl;
//pointer expression
*ptr=*ptr+1;
cout<<"The value of a after increment ptr = "<<a<<endl;
cout<<"The ptr pointing to address after increment = "<<ptr<<" which store value = "<<*ptr<<endl;
return 0;
}
```

**Output:**

#### Example #5 – Relational Expressions or Boolean expression

This expression results from a bool value either true or false after evaluating an expression. If relation expression contains a combination of arithmetic expressions then first arithmetic expressions are evaluated and then the results are compared.

Next, we write the C++ code to understand the relation expression more clearly with the following example –

**Code:**

```
#include <iostream>
using namespace std;
int main()
{
// declaration of variables
int a=2, b=3;
bool result;
//Relational expression
result = a*2>b;
// display result value
cout<<"The result of relation expression = "<<result;
return 0;
}
```

**Output:**

#### Example #6 – Logical Expressions

This expression combines two or more relational expressions by using &&’ and ‘||’ logical operators and gives a bool result which either true or false.

Next, we write the C++ code to understand the logical expression more clearly with the following example –

**Code:**

```
#include <iostream>
using namespace std;
int main()
{
// declaration of variables
int a=10, b=20, c=30;
bool result;
//Logical expression
result = (a<b)&&(b<c);
// display result value
cout<<"The result of logical expression = "<<result;
return 0;
}
```

**Output:**

#### Example #7 – Bitwise Expressions

This expression performs the operation at a bit level in an expression. The Bitwise Expressions and(&), or(|), not(~) and shift bits operators.

Next, we write the C++ code to understand the bitwise expression more clearly with the following example –

**Code:**

```
#include <iostream>
using namespace std;
int main()
{
// declaration of variables
int a=4;
int result;
//Logical expression first convert a=0100 2=0010 in binary and apply operators
result = a&2;
// display result value
cout<<"The result of bitwise expression for a&2 = "<<result<<endl;
result = a|2;
cout<<"The result of bitwise expression for a|2 = "<<result<<endl;
result = a>>2;
cout<<"The result of bitwise expression for a>>2 = "<<result<<endl;
return 0;
}
```

**Output:**

### Conclusion

C++ expression is an order collection of operators and operands which specifies a computation. There are different categories of an expression based on the operand and or result of the expression. Sometimes expressions can be compound expressions which is a combination of the above expressions.

### Recommended Articles

This is a guide to C++ Expression. Here we discuss the Examples of C++ Expression along with the different categories of expression. You may also have a look at the following articles to learn more –