Expression is a combination of variable, constant, operator, function calls which return values. Expression can contain one or more operands and 0 or more operators. These all are arranged according to the rules of C++. So, there are different types of expressions in C++.
Some of the basic types of Expressions are:
- Constant Expression
- Integral
- Float
- Pointer
- Relational
- Logical
- Bitwise
- Special Assignment Expressions
Constant Expressions:
These are expressions that only consist of constant value. Constant values mean those values which are already assigned. Below is an example program that shows constant expressions:
#include<iostream> using namespace std; int main() { int i; i= 54 + 44; cout<<"The value of i Is:"<<i; return 0; }
Integral Expressions:
It is an expression that gives a result after performing the explicit and implicit operations. Let us see 2 examples of integral expressions:
1st Example:
#include<iostream> using namespace std; int main() { int a, b, c; cout<<"Enter the values of a and b:"; cin>>a>>b; c=a+b; cout<<"\n value of c is:"<<c; return 0; }
2nd Example:
#include<iostream> using namespace std; int main(){ int a,b=3; a=(b*4)+8; cout<<"The value of a: "<<a; return 0; }
Float Expressions:
It’s an expression that generates a result in floating-point value after doing all the implicit and explicit tasks. So, below is an example of how to use float expressions.
Example:
#include <iostream> using namespace std; int main() { float a=6.4,b=3.2; float c; c=a*b; cout<<"Value of c is:" <<c; return 0; }
Pointer Expressions:
Pointer expression gives pointer value as an output of the conversions. Some examples of the pointer are: &x, ptr, ptr++, ptr-. Below is a code depicting the use of a pointer.
Example:
#include <iostream> using namespace std; int main() { int i = 8, *ptr; ptr = &i; cout << "Value of i is : "<< i << endl; cout << "Value of ptr is : "<< ptr<< endl; cout << "Value of *ptr is : "<< *ptr << endl; return 0; }
Relational Expressions:
These are the expressions that give Boolean type value which is either true or false. This is why they are also called Boolean expressions. So, basically what these do is a comparison between arithmetic expressions that are with these relational expressions.
Example:
#include <iostream> using namespace std; int main() { int i =15, j = 117; bool k; k = j > i; cout << "0 if false, 1 if true: "<<k << endl; return 0; }
Logical Expressions:
Logical expression combines 2 or more Boolean expressions which in result give Boolean type value. There are two logical expressions “&&” and “||”.
Example:
#include <iostream> using namespace std; int main() { int i=5, j=4, k=9; cout<<((i>j) && (j<k)); return 0; }
It is an expression used to shift bits, which means these are used to manipulate data at bit level.
Bitwise Expressions:
Example:
#include <iostream> using namespace std; int main() { int i=9; std::cout << (i<<2) << std::endl; return 0; }
Special Assignment Expressions:
These are the expressions which are further divided according to the value assigned to the variable.
Chained Assignment:
It is an expression in which in a single statement same value is assigned to more than one variable helping programmers to reduce the length of code and make it efficient. But we have to remember that we cannot use chained assignment while declaring the variables like i=k=3, this is a wrong statement.
Example:
#include <iostream> using namespace std; int main() { int j; int k,i; j=k=3; i=j*k; std::cout <<" product of j and k: "<<i<< std::endl; return 0; }
Embedded Assignment:
An embedded assignment expression is an expression that is enclosed within another assignment expression.
Example:
#include <iostream> using namespace std; int main() { int i, f; i=10*(f=6); cout <<"Values of 'i' is " <<i<<endl; return 0; }
Compound Assignment:
An expression in which a combination of binary operator and assignment operator.
Example:
#include <iostream> using namespace std; int main() { int i=90; i-=80; cout<< "Value of a is :" <<i<<endl; return 0; }
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.