C++ expressions consist of operators, constants, and variables arranged according to the rules of the language. They can also include function calls that return values. An expression can consist of one or more operands and zero or more operators to compute a value. Each expression produces a value that is assigned to a variable via the assignment operator.
Examples of C++ expressions:
(a+b) - c(x/y) -z4a2 - 5b +c(a+b) * (x+y)
Expressions can be of the following types:
-
Constant Expressions
-
Integer Expressions
-
Floating-point Expressions
-
Pointer Expressions
-
Relational Expressions
-
Logical Expressions
-
Bit Expressions
-
Special Assignment Expressions
If an expression is a combination of the above expressions, these are referred to as compound expressions.
Constant Expressions
A constant expression is an expression composed solely of constant values. It is determined at compile time but evaluated at runtime. It can consist of integer, character, floating-point, and enumeration constants. Advertisements
Constants are used in the following cases:
-
Used in subscript declarators to describe array boundaries.
-
Used after the case keyword in switch statements.
-
Used as numeric values in enums.
-
Specifying field width.
-
Used in preprocessor #if.
In the above cases, constant expressions can include integers, characters, and enumeration constants. We can define function scope with constants using the static and extern keywords.
đŸ‘‡ Click to claim đŸ‘‡
đŸ‘‰ C Language Knowledge Resource Collection
The following table shows expressions containing constant values:
Expressions Containing Constants | Constant Values |
---|---|
x = (2/3) * 4 | (2/3) * 4 |
extern int y = 67 | 67 |
int z = 43 | 43 |
static int a = 56 | 56 |
Let’s look at a simple program containing a constant expression:
#include <iostream>
using namespace std;
int main(){
int x; // Variable declaration.
x=(3/2) + 2; // Constant expression
cout<<"Value of x is:"<<x; // Display x's value.
return 0;
}
In the above code, we first declare the variable ‘x’ of integer type. After declaration, we assign a simple constant expression to the variable ‘x’.
Output
Value of x is: 3
Integer Expressions
An integer expression is an expression that produces an integer value as output after performing all explicit and implicit conversions.
Here are examples of integer expressions:
(x * y) - 5x + int(9.0) where x and y are the integers.
Let’s look at a simple example of an integer expression:
#include <iostream>
using namespace std;
int main(){
int x; // Variable declaration.
int y; // Variable declaration
int z; // Variable declaration
cout<<"Input values of x and y";
cin>>x>>y;
z=x+y;
cout<<"\n"<<"Value of z is:"<<z; // Display z's value.
return 0;
}
In the above code, we declared three variables, x, y, and z. After declaration, we accept user input for the values of x and y. Then, we add the values of x and y and store the result in the variable z.
Output
Enter the values of x and y 8 9
Value of z is: 17
Let’s look at another example of an integer expression.
#include <iostream>
using namespace std;
int main(){
int x; // Variable declaration
int y=9; // Variable initialization
x=y+int(10.0); // Integer expression
cout<<"Value of x is:"<<x; // Display x's value.
return 0;
}
In the above code, we declared two variables, x and y. We store the value of the expression (y+int(10.0)) in the variable ‘x’.
Output
Value of x: 19
Floating-point Expressions
A floating-point expression is an expression that produces a floating-point value as output after performing all explicit and implicit conversions.
Here are examples of floating-point expressions:
x+y(x/10) + y34.5x+float(10)
Let’s understand through an 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; // Display z's value.
return 0;
}
Output
Value of z is: 14.5
Let’s look at another example of a floating-point expression.
#include <iostream>
using namespace std;
int main(){
float x=6.7; // Variable initialization
float y; // Variable declaration
y=x+float(10); // Floating-point expression
std::cout <<"Value of y is:" << y <<std::endl; // Display y's value
return 0;
}
In the above code, we declared two variables, x and y. After declaration, we store the value of the expression (x+float(10)) in the variable ‘y’.
Output
Value of y is: 16.7
Pointer Expressions
A pointer expression is an expression that produces an address value as output.
Here are examples of pointer expressions:
&x ptr ptr++ ptr-
Let’s understand through an example.
#include <iostream>
using namespace std;
int main(){
int a[]={1,2,3,4,5}; // Array initialization
int *ptr; // Pointer declaration
ptr=a; // Assign the base address of the array to pointer ptr
ptr=ptr+1; // Increment the value of the pointer
std::cout <<"Value of the second element of the array:" << *ptr <<std::endl;
return 0;
}
In the above code, we declare an array and a pointer ptr. We assign the base address of the array to the variable ‘ptr’. After assigning the address, we increment the value of the pointer ‘ptr’. When the pointer is incremented, ‘ptr’ will point to the second element of the array.
Output
Value of second element of an array: 2
Relational Expressions
A relational expression is an expression that produces a bool type value, which can be true or false. It is also known as a boolean expression. When arithmetic expressions are used on both sides of a relational operator, the arithmetic expressions are evaluated first, and then their results are compared.
Here are examples of relational expressions:
a>b a-b >= x-y a+b>80
Let’s understand through an 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; // Display y's value.
return 0;
}
In the above code, we declared two variables, ‘a’ and ‘b’. After declaration, we apply the relational operator to check if ‘a’ is greater than ‘b’.
Output
Value of y is: 0
Let’s look at another 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;
}
In the above code, we declared four variables, ‘a’, ‘b’, ‘x’, and ‘y’. Then, we apply the relational operator (>=) among these variables.
Output
1
Logical Expressions
A logical expression is an expression that combines two or more relational expressions to produce a bool type value. The logical operators are ‘&&’ and ‘||’, used to combine two or more relational expressions.
Here are some examples of logical expressions:
a>b && x>y a>10 || b==5
Let’s look at a simple example of a logical expression.
#include <iostream>
using namespace std;
int main(){
int a=2;
int b=7;
int c=4;
cout<<((a>b)||(a>c));
return 0;
}
Output
0
Bit Expressions
A bit expression is an expression used to operate data at the bit level. They are primarily used to shift bits.
In the above example, the value of the variable ‘x’ is 3, whose binary value is 0011. We shift the value of ‘x’ three bits to the right. Let’s understand through a diagram representation.
Let’s look at a simple example.
#include <iostream>
using namespace std;
int main(){
int x=5; // Variable declaration
std::cout << (x>>1) << std::endl;
return 0;
}
In the above code, we declare a variable ‘x’. After declaration, we apply the bit operator, which is the right shift operator, to shift it one bit to the right.
Output
Value of x is: 2
Let’s look at another example.
#include <iostream>
using namespace std;
int main(){
int x=7; // Variable declaration
std::cout << (x<<3) << std::endl;
return 0;
}
In the above code, we declare a variable ‘x’. After declaration, we apply the left shift operator to shift the variable ‘x’ three bits to the left.
Output
Value of x is: 56
Special Assignment Expressions
Special assignment expressions are expressions classified further based on the values assigned to variables.
-
Chained Assignment
A chained assignment expression is an expression that assigns the same value to multiple variables in a single statement.
For example:
a=b=20 or (a=b) = 20
Let’s understand through an 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;
}
In the above code, we declared two variables, ‘a’ and ‘b’. Then, we use the chained assignment expression to assign the same value to both variables.
Output
Values of 'a' and 'b' are: 80, 80
Note: When using chained assignment expressions, you cannot assign values to variables when declaring them. For example, int a=b=c=90 is an invalid statement.
-
Nested Assignment Expressions
A nested assignment expression is an assignment expression nested within another assignment expression.
Let’s understand through an example.
#include <iostream>
using namespace std;
int main(){
int a; // Variable declaration
int b; // Variable declaration
a=10+(b=90); // Nested assignment expression
std::cout <<"Value of a is:" << a << std::endl;
return 0;
}
In the above code, we declared two variables, ‘a’ and ‘b’. Then, we apply the nested assignment expression (a=10+(b=90)).
Output
Value of a is 100
-
Compound Assignment
A compound assignment expression 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.
Let’s understand through an 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; // Display a's value.
return 0;
}
In the above code, we declared a variable ‘a’ and assigned it a value of 10. Then, we apply the compound assignment operator (+=) to the variable ‘a’, which is equivalent to (a=a+10). This statement increases the value of ‘a’ by 10.
Output
Value of a is: 20
Popular Recommendations
-
C Language Tutorial – C Language Program: Swap Two Numbers Without Using a Third Variable
-
C Language Algorithm – “Permutation” Algorithm Problem
-
C++ Tutorial – Identifiers in C++ Language