C++ Expressions

C++ Expressions are the combination of one or more operands and operators that are involved in the computation. Every expression produces a result that can be stored in a variable.

cpp expression

Below are a few examples of expression

(a+b) + c  
(a-b) + c  
2a + 4bc -5
(a+b) * (c+d) 
(a/b)+(c/d)

There are many types of c++ expressions depending on the type of operator used.

  1. Constant Expressions
  2. Integral Expressions
  3. Float Expressions
  4. Pointer Expressions
  5. Relational Expressions
  6. Logical Expressions
  7. Bitwise Expression
  8. Special Assignment Expressions

Let’s discuss one by one with examples


1. Constant Expressions

As its name indicated that the expression having only constant values is called constant expression. Below are some examples.

A=5+7;
C=67
X=8/2

Example: Constant expression program

//C++ Constant Expression 
#include<iostream>
#include <sstream>
using namespace std;
int main()
{
int a= 3;
a=(7+2) + 1;
cout<<"Value of a is : "<<a;
 	
}

Output

Value of a is : 10

2. Integral Expression

Integral expression produces only integer value as a result after computation.

Below are some examples of integral expressions.

Int a=6, b=4;
a+b+3
B+ int(2.0)

Example: Integral expression program


//Integral expression program in C++
#include<iostream>
#include <sstream>
using namespace std;
int main()
{
int a= 6, b=4;
int c;
c=a+b+3;
cout<<"Value of C is :"<<c; 
}

Output

Value of C is :13

3. Float Expression

It’s clear from its name that float expressions produce floating-point results while computing.

Below are a few examples

Float a =4.7, b=3.9;
A+b
A+b+b +float (4)

Example: Float expression program

//float expression program in C++
#include<iostream>
#include <sstream>
using namespace std;
int main()
{
float a= 4.7, b=3.9;
float c;
c=a+b+3;
cout<<"Value of C is: "<<c; 
}

Output

Value of C is: 11.6

4. Pointer expression

Pointer expression produces address values during computation.

Below are some examples

&a
Ptr++
Ptr--

Example: Pointer expression program

//Pointer expression in C++
#include<iostream>
#include <bits/stdc++.h>  
using namespace std;  
int main()  
{  
   int a;   
   int arr[]={1,2,3,4,5,6,7};  
   int *ptr;      
   ptr = arr;    
  cout<<"Enter element of an array: ";
  cin>>a;
   ptr = ptr + a;   
   cout <<"Value of element "<<a<<" is: "  << *ptr<<endl;  
   cout <<"Address of element "<<a<<" is: "  << ptr<<endl;  
}

Output

Enter element of an array: 4
Value of element 4 is: 5
Address of element 4 is: 0x6ffdf0

5. Relational expression

Relational expression produces true or false results during computation. These are also called Boolean expressions.

a<b
a+b >c+d
a+b<30

Example: Relational expression program

// Relational expression in C++
#include <bits/stdc++.h>  
using namespace std;  
int main()  
{  
   int a=6,b=4;
   bool c;
  
   c= a < b; 
   cout << "Value of c is: "<<c<< endl; 
   c = a+b < 30; 
   cout << "Now value of c is: "<<c << endl;
 
} 

Output

Value of c is: 0
Now value of c is: 1

6. Logical Expression

Logical Expression involves logical operators (“&&”,”||” and “!”) to produce a Boolean result true or false.

Below are some examples

a>b && A<C
A<10 ||B>10

Example: Logical expression in C++

// Logical expression in C++
#include <iostream>  
using namespace std;  
int main()  
{  
 int a=4, b=8;  
 int c=4;  
cout<<"Return value: "<<((a>10)||(b<10));  
return 0;  
} 

Output

Return value: 1

7. Bitwise Expression

Bitwise expression is used to compute data at the bit level. It is required to move bits ether left and right using left shift “<<” and right shift “>>”.

X>>3
X<<1

Example: Bitwise Expression

//Bitwise expression in C++
#include <iostream>  
using namespace std;  
int main()  
{  
 int a=4, b=8;
 
  a = a>>1; 
cout <<"Value of a is: "<<a<<endl;  
b = b<<1; 
cout <<"Value of b is: "<<b<<endl;  
return 0;  
} 

Output


Value of a is: 2
Value of b is: 16

7. Special Assignment Expressions

Special Assignment Expressions further divided into three more types

  1. Chained Assignment Expression
  2. Embedded Assignment Expression
  3. Compound Assignment Expression

Chained Assignment Expression

In this type, we can assign one value to the multiple variables in a single statement.

Example

A=B=6
X=Y=100

Embedded Assignment Expression

In this type, the assignment expression encloses with another assignment expression.

Example

A= B +(C=21)
C= (A=4) + (B=2)

Compound Assignment Expression

In this type of assignment expression and the binary expression are used simultaneously.

Example

A += B + (C=21)

Scroll to Top