What is Expression in C++ | Expression in C++

Share Your Love

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:

  1. Constant Expression
  2. Integral
  3. Float
  4. Pointer
  5. Relational
  6. Logical
  7. Bitwise
  8. 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;
}
bLVwwQ6Q1ecDd4gsM11ZlKRXZdsH1gKQssHjSbdc3 j1GuvrEpRB me AL9OrhJ8fe9Ph8piODYtz58L0Q50 SulXUy AI0rEYNFmGrofYXyKFlzVRFErrg 8W AE3j6IU3mw
Credit Goes To Shiva Das

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;
}
3Sb6Xy7WbxCjv3C0vF bapVmr59yXmrqzIJrpSsvnWNxDaW4Jx68XOyKCbcomqhNsxC3HWCvXLZW80x0lQFaaCfIV6kRNcz81HKEuoxvx H6RpW3DyOpWeveyDMd vUkPOBRw
Credit Goes To Shiva Das

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;
}
2e31aOAlh7eX84pkVlrZCkrZrCHuZvmhNPZP23aO1lHndTUIHx8DZLvO2BrKb ltQRN7yKcWEn8OC0iBh1n1Tr4dGi0BOvrbGGT0wtqy YXDuTgnGtg FjZIaHds8ZdUjNG3aA
Credit Goes To Shiva Das

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;  
}
CuzR60To0V W4asf BLYLVt8Nid2gv84O8X IfhvzGmT2O8mhoo47BXcrK8tkiWdl sSLw065n6GOYs8xaULmRDsZGIlp QJadEIQDpADWhJHEiRJoKFVQZPSVP65lVmnQDYPA
Credit Goes To Shiva Das

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;
}
nQ7Oe50XvEqcjvVVvmF61Ls9WTUXhVDeH2ypnWprT gi1sY2oY2ysmzEfXkDuhDfzNf4XBXM4NtqBcDmbljwDsbNRmQ ZhHEu2FNKNZ8x2dOVbpNCTpx jLXEwD70BTnD3youg
Credit Goes To Shiva Das

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;
}
iv E0oqtcV4qWuCfVTEl1keka9nJnMqaHCWONHCJalZ61h9ZOA7ZdiFR4fY042tKqd2MOega96B6lA5Eydeonkyz5HnbCE00TIHjveytQCvBwVfdz 5f 33pRUE1BMIKT3SdGA
Credit Goes To Shiva Das

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;  
} 
Credit Goes To Shiva Das

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;  
} 
YJ6FqmS9kX Y RuZSrysGJnMe6vu FGeJy Rj T4l2xTHfThMnOKxv6qmTtI jCqteqTtl8R Qrbim JHRHURLJAioiWzQhZGBSrYuRtTXcW6aNoviWnfkMiPSw3LyUr9hjug
Credit Goes To Shiva Das

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;  
}
2dwVCZD3wy89XE7b95iGYnKfHb 2ykjUmS jpwLeLmz0cUMVpF2a82hBSq4
Credit Goes To Shiva Das

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;  
} 
fKWTCHiWovNWm4gHmFCKoKgINTriqEnWoIiRF1eO7hUfGxKWC
Credit Goes To Shiva Das

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;  
}
o4ClQakWuj1ettLlAQKibUQBlqHFr1FRHJSgLOlinqv4YrrzdBr5KHrrFyPLoMfnq1B1S1aTSPhhgHhh5biaDjHrCGIbbbpMMlj
Credit Goes To Shiva Das

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Shiva Patra
Shiva Patra

Hello, this is G. Shiva. Patra, your content writer for this website, is currently pursuing BCA for a graduation degree. I am here to write content based on C++ and similar kind of stuff.
I like the tech world as it grows really fast and you have great things to learn every day which keep you away from boredom. So, I have some knowledge on very different topics related to tech like the dark web, deep web, onion rooting, IoT, Cybersecurity, and I always keep track of new devices and software used by them as every tech enthusiast does.

Articles: 9

Newsletter Updates

Enter your email address below to subscribe to our newsletter