Hi everyone, inside this article we will see about C++ Expressions.

In C++, an expression is a combination of variables, operators, and values that can be evaluated to a single value. Expressions can be used to assign values to variables, to perform arithmetic or logical operations, or as part of control statements (e.g. if, while).

For example, the expression `2 + 3`

evaluates to the value 5, and the expression `x = 2 + 3`

assigns the value 5 to the variable `x`

. Expressions can also include function calls, type casts, and conditional operator (ternary operator). The value of an expression can be of any data type, such as an integer, floating-point number, or character.

## Types of C++ Expression

C++ has several types of expressions, including:

**Arithmetic** Expressions: Expressions that perform arithmetic operations such as addition, subtraction, multiplication, and division.

**Relational** Expressions: Expressions that compare two values and return a boolean result indicating whether the comparison is true or false.

**Logical** Expressions: Expressions that use the logical operators (&&, ||, !) to combine relational expressions.

**Assignment** Expressions: Expressions that assign a value to a variable.

**Conditional** Expressions: Expressions that use the ternary operator (?: ) to choose between two values based on a condition.

**Function Call** Expressions: Expressions that call a function and return its result.

**Cast** Expressions: Expressions that convert one data type to another.

**Comma** Expressions: Expressions that use the comma operator (,) to evaluate multiple expressions and return the value of the last expression.

**Sizeof** Expressions: Expressions that return the size of a data type or object in bytes.

**Member Access** Expressions: Expressions that access a member of a class or structure.

These are just some of the types of expressions that can be used in C++. The type of expression used will depend on the specific requirements of the program.

Here, is a pictorial view of types of C++ expressions.

## Examples

Let’s see some examples.

**Arithmetic Expressions**

Here is a simple C++ program that demonstrates the use of **arithmetic expressions**:

#include <iostream> using namespace std; int main() { int x = 10, y = 20; // Addition cout << "x + y = " << x + y << endl; // Subtraction cout << "x - y = " << x - y << endl; // Multiplication cout << "x * y = " << x * y << endl; // Division cout << "x / y = " << x / y << endl; // Modulus cout << "x % y = " << x % y << endl; return 0; }

This program declares two integer variables `x`

and `y`

, and performs several arithmetic operations on them. The results of the operations are displayed using the `cout`

statement and the `endl`

manipulator.

**Output**

```
x + y = 30
x - y = -10
x * y = 200
x / y = 0
x % y = 10
```

The program demonstrates the use of the addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) operators. Note that the division operator performs integer division, meaning that the result is rounded down to the nearest integer.

### Relational Expressions

Here is a simple C++ program that demonstrates the use of **relational expressions**:

#include <iostream> using namespace std; int main() { int x = 10, y = 20; // Equal to cout << "x == y: " << (x == y) << endl; // Not equal to cout << "x != y: " << (x != y) << endl; // Less than cout << "x < y: " << (x < y) << endl; // Greater than cout << "x > y: " << (x > y) << endl; // Less than or equal to cout << "x <= y: " << (x <= y) << endl; // Greater than or equal to cout << "x >= y: " << (x >= y) << endl; return 0; }

This program declares two integer variables `x`

and `y`

, and performs several relational operations on them. The results of the operations are displayed using the `cout`

statement and the `endl`

manipulator.

**Output**

```
x == y: 0
x != y: 1
x < y: 1
x > y: 0
x <= y: 1
x >= y: 0
```

The program demonstrates the use of the equal to (==), not equal to (!=), less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=) relational operators. The relational operations return a boolean value indicating whether the comparison is true.

### Assignment Expressions

Here is a simple C++ program that demonstrates the use of **assignment expressions**:

#include <iostream> using namespace std; int main() { int x = 10; int y = 20; cout << "x = " << x << endl; cout << "y = " << y << endl; // Simple assignment x = y; cout << "x = y: x = " << x << endl; // Add and assign x += y; cout << "x += y: x = " << x << endl; // Subtract and assign x -= y; cout << "x -= y: x = " << x << endl; // Multiply and assign x *= y; cout << "x *= y: x = " << x << endl; // Divide and assign x /= y; cout << "x /= y: x = " << x << endl; // Modulus and assign x %= y; cout << "x %= y: x = " << x << endl; return 0; }

This program declares two integer variables `x`

and `y`

, and performs several assignment operations on `x`

. The results of the operations are displayed using the `cout`

statement and the `endl`

manipulator.

**Output**

```
x = 10
y = 20
x = y: x = 20
x += y: x = 40
x -= y: x = 20
x *= y: x = 400
x /= y: x = 20
x %= y: x = 0
```

The program demonstrates the use of simple assignment (=), add and assign (+=), subtract and assign (-=), multiply and assign (*=), divide and assign (/=), and modulus and assign (%=) operators. These operators allow you to perform an operation and assign the result to a variable in a single statement.

### Cast Expressions

Here is a simple C++ program that demonstrates the use of **cast expressions**:

#include <iostream> using namespace std; int main() { float x = 10.5; int y = 20; // Cast float to int int z = (int)x; cout << "z = (int)x: z = " << z << endl; // Cast int to float float w = (float)y; cout << "w = (float)y: w = " << w << endl; return 0; }

This program declares a float variable `x`

and an integer variable `y`

, and performs two cast operations. The results of the operations are displayed using the `cout`

statement and the `endl`

manipulator.

**Output**

```
z = (int)x: z = 10
w = (float)y: w = 20
```

The program demonstrates the use of cast expressions. A cast expression allows you to convert a value from one data type to another. In this program, the cast expression `(int)x`

converts the value of `x`

from a float to an integer, and the cast expression `(float)y`

converts the value of `y`

from an integer to a float.

### Sizeof Expressions

Here is a simple C++ program that demonstrates the use of `sizeof`

expressions:

#include <iostream> using namespace std; int main() { int x = 10; float y = 20.5; char z = 'A'; // Size of int cout << "sizeof(int) = " << sizeof(int) << " bytes" << endl; // Size of float cout << "sizeof(float) = " << sizeof(float) << " bytes" << endl; // Size of char cout << "sizeof(char) = " << sizeof(char) << " bytes" << endl; // Size of variable x cout << "sizeof(x) = " << sizeof(x) << " bytes" << endl; // Size of variable y cout << "sizeof(y) = " << sizeof(y) << " bytes" << endl; // Size of variable z cout << "sizeof(z) = " << sizeof(z) << " bytes" << endl; return 0; }

This program declares three variables: an integer `x`

, a float `y`

, and a character `z`

. The `sizeof`

operator is used to determine the size, in bytes, of each data type and each variable. The results of the `sizeof`

expressions are displayed using the `cout`

statement and the `endl`

manipulator.

**Output**

```
sizeof(int) = 4 bytes
sizeof(float) = 4 bytes
sizeof(char) = 1 bytes
sizeof(x) = 4 bytes
sizeof(y) = 4 bytes
sizeof(z) = 1 bytes
```

The `sizeof`

operator returns the size, in bytes, of the data type or expression that follows it. This is useful when you need to allocate memory dynamically or when you want to ensure that a data type will be able to store the values that you need. Note that the size of data types and variables can vary depending on the implementation, the operating system, and the hardware architecture.

We hope this article helped you to understand about **C++ Expressions** in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & itâ€™s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.