A C++ expression is comprised of operators, constants, and variables, organized in compliance with the language's syntax. It may include function invocations that yield results. Expressions are formed by combining one or more operands with zero or more operators to calculate a final value. Each expression generates a value that can be stored in a variable using an assignment operator.
Basic Syntax of C++ Expression
(a+b) - c
(x/y) -z
4a2 - 5b +c
(a+b) * (x+y)
Here, we have selected various expressions that carry out multiple tasks in C++.
Example of C++ expression
Let's consider a straightforward example to illustrate C++ Expressions.
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 given expression is a mix of the expressions mentioned earlier, these are referred to as compound expressions.
Special Assignment Expressions
Special task expressions are expressions that can be categorized further based on the assigned value to the variable.
- Sequential Assignment
A chained assignment expression involves assigning a single value to multiple variables within one statement.
For example:
a=b=20
or
(a=b) = 20
C++ Chained Assignment Example:
Let's consider an example to demonstrate the concept of 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 provided code snippet, we defined a pair of variables named 'a' and 'b'. Following this, we proceeded to assign identical values to both variables through a cascaded assignment statement.
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.
- In-line Assignment Expression
An embedded assignment expression is a type of assignment expression where one assignment expression is nested inside another assignment expression.
C++ Embedded Assignment Expression Example:
Let's consider an example to demonstrate 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 provided code snippet, we initialized two variables, namely 'a' and 'b'. Subsequently, we executed a compound assignment expression (a=10+(b=90)).
- Compound Assignment
A compound assignment expression combines an assignment operator with a binary operator to form a single expression.
For example,
a+=10;
In the previous assertion, 'a' represents a variable and the operator '+=' signifies a compound statement.
C++ Compound Assignment Example:
Let's consider an illustration to demonstrate the compound assignment operators 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 provided code snippet, a variable 'a' is initialized with a value of 10. Subsequently, the compound assignment operator (+=) is used on the 'a' variable, i.e., a+=10, which is essentially equivalent to (a=a+10). This particular statement results in increasing the value of 'a' by 10.
Constant Expressions
A constant expression is an expression made up of unchanging values. It represents a value that is established during compilation but assessed during program execution. This type of expression can include integer, character, floating-point, and enumeration constants.
Constant expressions are allowed to consist of integer, character, and enumeration constants. The function scope can be defined by incorporating the static and extern keywords with these constants.
The subsequent table displays the expression incorporating a fixed 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 explore a basic code snippet to demonstrate the constant expression concept in the C++ programming language.
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 provided code snippet, we initially define the variable 'x' as an integer data type. Following the declaration, we proceed to assign a basic constant expression to the variable 'x'.
Integral Expressions
An integral expression is a mathematical statement that results in an integer value when all explicit and implicit conversions are carried out.
Here are some instances of integral expressions:
(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 explore a basic illustration of an 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 provided code snippet, three variables are defined: x, y, and z. Subsequently, user input is obtained for the variables 'x' and 'y'. Following this, the sum of 'x' and 'y' is computed and saved in the variable 'z'.
Float Expressions
A float expression is an expression that generates a floating-point value as a result after executing both 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's consider a scenario to demonstrate the float data type 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 provided code snippet, two variables, namely x and y, are defined. Subsequently, the outcome of the expression (x+float(10)) is assigned to the variable 'y'.
Pointer Expressions
A pointer expression is an expression that generates a memory location value as its result.
Some instances of pointer expressions include:
&x
ptr
ptr++
ptr-
C++ Pointer Expressions Example:
Let's consider an example to demonstrate the pointer notation 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 provided code snippet, an array and a pointer named ptr are initialized. The base address is assigned to the variable 'ptr'. Following the assignment, we proceed to increment the pointer's value. This action results in the pointer 'ptr' pointing to the second element within the array.
Relational Expressions
A relational expression is an expression that generates a boolean value, representing either true or false. This type of expression is also referred to as a boolean expression. When arithmetic expressions are present on each side of the relational operator, the arithmetic expressions are computed initially, followed by a comparison of their outcomes.
Below are instances of the relational expression:
a>b
a-b >= x-y
a+b>80
C++ Relational Expression Example:
Let's consider an example to demonstrate the relational operator 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 provided code snippet, we defined two variables, namely 'a' and 'b'. Following their declaration, we used the relational operator to compare if 'a' is larger than 'b'.
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 provided code snippet, we have initialized four variables namely 'a', 'b', 'x', and 'y'. Following this initialization, we utilize the relational operator (>=) to compare these variables.
Logical Expressions
A logical expression is an expression that merges multiple relational expressions to generate a boolean value. The logical operators '&&' and '||' are utilized to combine two or more relational expressions.
Below are a few instances of logical statements:
a>b && x>y
a>10 || b==5
C++ Logical Expression Example:
Let's consider an example to demonstrate the logical expression within 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 that operates on data at the bit level, enabling manipulation of individual bits by performing operations such as shifting.
For example:
x=3
x>>3 // This statement means that we are shifting the three-bit position to the right.
In the given instance, the variable 'x' holds the numerical value of 3, represented in binary as 0011. The operation involves shifting the 'x' value three bits to the right. This process can be better comprehended with the assistance of a visual representation.
C++ Bitwise Expression Example:
Let's consider a basic example to demonstrate the bitwise operation 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 provided code snippet, a variable 'x' is initialized. Following this initialization, the bitwise right shift operator is employed to shift the binary representation of 'x' by one position to the 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 provided code snippet, a variable 'x' is initialized. Following initialization, a left shift operation is performed on variable 'x' to move it three bits to the left.
In C++, what will be the result of the expression provided below?
x= 7 + 8 * 3;
What will be the result of the given bitwise expression in the C++ programming language?
int x = 10, y = 15;
cout <<(x & y);
(a) 10
What is the data type of the given expression in C++?
x > y && y < z
- Arithmetic Expression
- Assignment Expression
- Logical Expression
- Bitwise Expression
- What is the result of the given 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