C++ Expressions with easy Explanation

0 0
Read Time:6 Minute, 56 Second

C++ Expressions

In C++, Expression consists of operators, constants, and variables which are arranged according to the rules of the language. It can also contain function calls which return values. An expression can consist of one or more operands, zero or more operators to compute a value. Every expression produces some value which is assigned to the variable with the help of an assignment operator.

An expression can be of following Types:

  1. Constant expressions
  2. Integral expressions
  3. Float expressions
  4. Pointer expressions
  5. Relational expressions
  6. Logical expressions
  7. Bitwise expressions
  8. Special assignment expressions

Constant expressions

A constant expression is an expression that consists of only constant values. It is an expression whose value is determined at the compile-time but evaluated at the run-time. It can be composed of integer, character, floating-point, and enumeration constants.

Constants are used in the following situations:

  • It is used in the subscript declarator to describe the array bound.
  • It is used after the case keyword in the switch statement.
  • It is used as a numeric value in an enum
  • It specifies a bit-field width.
  • It is used in the pre-processor #if

In the above scenarios, the constant expression can have integer, character, and enumeration constants. We can use the static and extern keyword with the constants to define the function-scope.

The following table shows the expression containing constant value:

Expression containing constantConstant value
x = (2/3) * 4(2/3) * 4
extern int y = 6767
int z = 4343
static int a = 5656

Let’s see a simple program containing 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; // displaying the value of x.  
    return 0;  
};

Output

Value of x is : 3  

Integral Expressions

An integer expression is an expression that produces the integer value as output after performing all the explicit and implicit conversions.

Following are the examples of integral expression:

(x * y) -5     
x + int(9.0)  
where x and y are the integers.

Let’s see a simple example of integral expression:

#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     

Let’s see another example of integral expression.

#include <iostream> 
using namespace std; 
int main()  {     
   int x;   // variable declaration  
   int y=9;    // variable initialization 
   x=y+int(10.0);    // integral expression
   cout<<"Value of x : "<<x;   // displaying the value of x.
   return 0;  
};

Output

Value of x : 19

Float Expressions

A float expression is an expression that produces floating-point value as output after performing all the explicit and implicit conversions.

The following are the examples of float expressions:

x+y  
(x/10) + y  
34.5  
x+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;  
   cout <<"value of z is :"  << z<<endl;  // displaying the value of z.  
   return 0;  
};

Output

value of z is :14.5    

Let’s see another example of float expression.

#include <iostream>  
using namespace std; 
int main()  {
   float x=6.7;    // variable initialization  
   float y;      // variable declaration  
   y=x+float(10);   // float expression  
   cout <<"value of y is :"  << y<<endl;  // displaying the value of y  
   return 0;  
};

Output

value of y is :16.7  

Pointer Expressions

A pointer expression is an expression that produces address value as an output.

The following are the examples of pointer expression:

&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;    // assigning base address of array to the pointer ptr  
   ptr=ptr+1;   // incrementing the value of pointer  
   cout <<"value of second element of an array : "  << *ptr<<endl;  
   return 0;  
};

Output

value of second element of an array : 2

Relational Expressions

A relational expression is an expression that produces a value of type bool, which can be either true or false. It is also known as a boolean expression. When arithmetic expressions are used on both sides of the relational operator, arithmetic expressions are evaluated first, and then their results are compared.

The following are the examples of the relational expression:

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;  // displaying the value of y.  
    return 0;  
};

Output

Value of y is :0 

Let’s see 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;  
};

Output

1

Logical Expressions

A logical expression is an expression that combines two or more relational expressions and produces a bool type value. The logical operators are ‘&&’ and ‘||’ that combines two or more relational expressions.

The following are some examples of logical expressions:  

a>b && x>y 
a>10 || b==5

Let’s see a simple example of 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

Bitwise Expressions

A bitwise expression is an expression which is used to manipulate the data at a bit level. They are basically used to shift the bits.

For example:

x=3
x>>3 // This statement means that we are shifting the three-bit position to the right.

Let’s see a simple example.

#include <iostream>  
using namespace std; 
int main()  {  
 int x=5;   // variable declaration  
 cout << (x>>1) << std::endl;  
 return 0;  
};

Output

2

Let’s look at another example.

#include <iostream>  
using namespace std; 
int main()  {  
 int x=7;   // variable declaration  
 cout << (x<<3) << std::endl;  
 return 0;  
};

Output

56

Special Assignment Expressions

Special assignment expressions are the expressions which can be further classified depending upon the value assigned to the variable.

  • Chained Assignment

Chained assignment expression is an expression in which the same value is assigned to more than one variable by using 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  
 cout <<"Values of 'a' and 'b' are : " <<a<<","<<b<<endl;  
 return 0;  
};

Output

Values of 'a' and 'b' are : 80,80  

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.

Embedded Assignment Expression

An embedded assignment expression is an assignment expression in which assignment expression is enclosed 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);  // embedded assignment expression  
 cout <<"Values of 'a' is " <<a<<endl;  
 return 0;  
};

Output

Values of 'a' is 100  

Compound Assignment

A compound assignment expression is an expression which is a combination of an assignment operator and binary operator.

For example,

a+=10;

Let’s understand through an example.

#include <iostream>  
using namespace std; 
int main(){  
  int a=10;   // variable declaration  
  a+=10;    // compound assignment  
  cout<<"Value of a is :"<<a<<endl; // displaying the value of a.  
  return 0;  
};

Output

Value of a is :20
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “C++ Expressions with easy Explanation

Leave a Comment