C++ expression consists of operators , constants, and variables , which are arranged according to the rules of the language. It can also contain function calls, which return values. An expression can consist of one or more operands and zero or more operators to compute a value. Every expression produces some value, which is assigned to the variable with the help of an assignment operator.
Basic Syntax of C++ Expression
(a+b) - c
(x/y) -z
4a2 - 5b +c
(a+b) * (x+y)
Here, we have taken the several expressions that performs multiple operations in C++.
Example of C++ expression
Let us take a simple example to explain C++ Expression.
Example
#include <iostream>
using namespace std; // Use standard namespace
int main() {
int x = 12, y = 3; // Declaring integer variables
int addition = x + y; // addition expression
int division = x / y; // Division expression
cout << "Addition: " << addition << endl; //print the Addition
cout << "Divison: " << division << endl; //print the Division
return 0;
}
Output:
Addition: 15
Divison: 4
Types of C++ Expression
Several types of Expressions in C++ are as follows:
- Special assignment expressions
- Constant expressions
- Integral expressions
- Float expressions
- Pointer expressions
- Relational expressions
- Logical expressions
- Bitwise expressions
If the expression is a combination of the above expressions, such expressions are known as compound expressions.
Special Assignment Expressions
Special assignment expressions are expressions that can be further classified depending on the value assigned to the variable.
- Chained Assignment
A chained assignment expression is an expression in which the same value is assigned to more than one variable by using a single statement.
For example:
a=b=20
or
(a=b) = 20
C++ Chained Assignment Example:
Let's take an example to illustrate the chained assignment in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int a; // variable declaration
int b; // variable declaration
a=b=80; // chained assignment
std::cout <<"Values of 'a' and 'b' are : " <<a<<","<<b<< std::endl;
return 0;
}
Output:
Values of 'a' and 'b' are : 80,80
Explanation:
In the above code, we have declared two variables, i.e., 'a' and 'b'. After that, we assigned the same value to both variables using a chained assignment expression.
Note: Using chained assignment expression, the value cannot be assigned to the variable at the time of declaration. For example, int a=b=c=90 is an invalid statement.
- Embedded Assignment Expression
An embedded assignment expression is an assignment expression in which assignment expression is enclosed within another assignment expression.
C++ Embedded Assignment Expression Example:
Let's take an example to illustrate the embedded assignment expression in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int a; // variable declaration
int b; // variable declaration
a=10+(b=90); // embedded assignment expression
std::cout <<"Values of 'a' is " <<a<< std::endl;
return 0;
}
Output:
Values of 'a' is 100
Explanation:
In the above code, we have declared two variables, i.e., 'a' and 'b'. After that, we applied embedded assignment expression (a=10+(b=90)).
- Compound Assignment
A compound assignment expression is an expression that is a combination of an assignment operator and a binary operator.
For example,
a+=10;
In the above statement, 'a' is a variable and '+=' is a compound statement.
C++ Compound Assignment Example:
Let's take an example to illustrate the compound assignment in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int a=10; // variable declaration
a+=10; // compound assignment
std::cout << "Value of a is :" <<a<< std::endl; // displaying the value of a.
return 0;
}
Output:
Value of a is :20
Explanation:
In the above code, we have declared a variable 'a' and assigned 10 values to this variable. After that, we applied the compound assignment operator (+=) to the 'a' variable, i.e., a+=10, which is equal to (a=a+10). This statement increments the value of 'a' by 10.
Constant Expressions
A constant expression is an expression that consists of only constant values. It is an expression whose value is determined at the compile-time but evaluated at the run-time. It can be composed of integer , character , floating-point, and enumeration constants.
The constant expression can have integer, character, and enumeration constants. We can use the static and extern keywords with the constants to define the function scope.
The following table shows the expression containing a constant value:
| Expression containing constant | Constant value |
|---|---|
| x = (2/3) * 4 | (2/3) * 4 |
| extern int y = 67 | 67 |
| int z = 43 | 43 |
| static int a = 56 | 56 |
C++ Constant Expression Example:
Let's see a simple program to illustrate the constant expression in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int x; // variable declaration.
x=(3/2) + 2; // constant expression
cout<<"Value of x is : "<<x; // displaying the value of x.
return 0;
}
Output:
Value of x is : 3
Explanation:
In the above code, we have first declared the 'x' variable of integer type. After declaration, we assign the simple constant expression to the 'x' variable.
Integral Expressions
An integeral expression is an expression that produces the integer value as output after performing all the explicit and implicit conversions.
Following are the examples of integral expression:
(x * y) -5
x + int(9.0)
where x and y are the integers.
Where x and y are the integers.
C++ Integral Expressions Example:
Let's see a simple example of integral expression in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int x; // variable declaration.
int y; // variable declaration
int z; // variable declaration
cout<<"Enter the values of x and y";
cin>>x>>y;
z=x+y;
cout<<"\n"<<"Value of z is :"<<z; // displaying the value of z.
return 0;
}
Output:
Enter the values of x and y
8
9
Value of z is :17
Explanation:
In the above code, we have declared three variables, i.e., x, y, and z. After declaration, we take the user input for the values of 'x' and 'y'. After that, we add the values of 'x' and 'y' and store their result in the 'z' variable.
Float Expressions
A float expression is an expression that produces a floating-point value as output after performing all the explicit and implicit conversions.
The following are examples of float expressions:
x+y
(x/10) + y
34.5
x+float(10)
C++ Float Expressions Example:
Let us take an example to illustrate the float expressions in C++.
Example
#include <iostream>
using namespace std;
int main()
{
float x=8.9; // variable initialization
float y=5.6; // variable initialization
float z; // variable declaration
z=x+y;
std::cout <<"value of z is :" << z<<std::endl; // displaying the value of z.
return 0;
}
Output:
value of z is :14.5
Let's see another example of float expression.
Example
#include <iostream>
using namespace std;
int main()
{
float x=6.7; // variable initialization
float y; // variable declaration
y=x+float(10); // float expression
std::cout <<"value of y is :" << y<<std::endl; // displaying the value of y
return 0;
}
Output:
value of y is :16.7
Explanation:
In the above code, we have declared two variables, i.e., x and y. After declaration, we store the value of expression (x+float(10)) in variable 'y'.
Pointer Expressions
A pointer expression is an expression that produces an address value as an output.
The following are examples of pointer expressions:
&x
ptr
ptr++
ptr-
C++ Pointer Expressions Example:
Let's take an example to illustrate the pointer expression in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int a[]={1,2,3,4,5}; // array initialization
int *ptr; // pointer declaration
ptr=a; // assigning base address of array to the pointer ptr
ptr=ptr+1; // incrementing the value of pointer
std::cout <<"Value of second element of an array : " << *ptr<<std::endl;
return 0;
}
Output:
Value of second element of an array : 2
Explanation:
In the above code, we declare the array and a pointer ptr. We assign the base address to the variable 'ptr'. After assigning the address, we increment the value of the pointer 'ptr'. When the pointer is incremented, then 'ptr' will be pointing to the second element of the array.
Relational Expressions
A relational expression is an expression that produces a value of type bool, which can be either true or false. It is also known as a boolean expression. When arithmetic expressions are used on both sides of the relational operator, arithmetic expressions are evaluated first, and then their results are compared.
The following are the examples of the relational expression:
a>b
a-b >= x-y
a+b>80
C++ Relational Expression Example:
Let's take an example to illustrate the relational expression in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int a=45; // variable declaration
int b=78; // variable declaration
bool y= a>b; // relational expression
cout<<"Value of y is :"<<y; // displaying the value of y.
return 0;
}
Output:
Value of y is :0
Explanation:
In the above code, we have declared two variables, i.e., 'a' and 'b'. After declaration, we have applied the relational operator between the variables to check whether 'a' is greater than 'b' or not.
Let's see another example.
Example
#include <iostream>
using namespace std;
int main()
{
int a=4; // variable declaration
int b=5; // variable declaration
int x=3; // variable declaration
int y=6; // variable declaration
cout<<((a+b)>=(x+y)); // relational expression
return 0;
}
Output:
Explanation:
In the above code, we have declared four variables, i.e., 'a', 'b', 'x' and 'y'. After that, we apply the relational operator (>=) between these variables.
Logical Expressions
A logical expression is an expression that combines two or more relational expressions and produces a bool-type value. The logical operators are '&&' and '||', which combine two or more relational expressions.
The following are some examples of logical expressions:
a>b && x>y
a>10 || b==5
C++ Logical Expression Example:
Let's take an example to illustrate the logical expression in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() //main method
{
int a=2; //assign the value of a
int b=7; //assign the value of b
int c=4; //assign the value of c
cout<<((a>b)||(a>c)); //using logical expression
return 0;
}
Output:
Bitwise Expressions
A bitwise expression is an expression, which is used to manipulate the data at a bit level. They are basically used to shift the bits.
For example:
x=3
x>>3 // This statement means that we are shifting the three-bit position to the right.
In the above example, the value of 'x' is 3, and its binary value is 0011. We are shifting the value of 'x' by a three-bit position to the right. Let's understand through the diagrammatic representation.
C++ Bitwise Expression Example:
Let's take an simple example to illustrate the bitwise expression in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int x=5; // variable declaration
std::cout << (x>>1) << std::endl;
return 0;
}
Output:
Explanation:
In the above code, we have declared a variable 'x'. After declaration, we applied the bitwise operator, i.e., right shift operator to shift one-bit position to right.
Let's look at another example.
Example
#include <iostream>
using namespace std;
int main()
{
int x=7; // variable declaration
std::cout << (x<<3) << std::endl;
return 0;
}
Output:
Explanation:
In the above code, we have declared a variable 'x'. After declaration, we applied the left shift operator to variable 'x' to shift the three-bit position to the left.
C++ Expression MCQs:
- What would be the output of the following expression in C++?
x= 7 + 8 * 3;
- What would be the output of the following bitwise expression in C++?
int x = 10, y = 15;
cout <<(x & y);
- What is the type of the following expression in C++?
x > y && y < z
- Arithmetic Expression
- Assignment Expression
- Logical Expression
- Bitwise Expression
- What is the output of the following expressions in C++?
int a = 5;
cout << (++a * 10);
- Which of the following options shows the correct expression in C++?
- (x + y) * z;
- (7 - 2) / 5;
- (a * b) - c;
- All of the Above