expressions-in-cpp

Expressions in C++

In this article, we are going to learn about C++ Expression.

In C++ expression the operators, constants, and variables are present. These are arranged according to the rules of the language.
It can also contain function calls that return values.
An expression can consist of one or more operands, and zero or more operators to compute a value.
With the help of the assignment operator, a value is produced by the expression which is assigned to a variable.

There are 8 different types of Expressions: 

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

1.) Constant expressions:

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

Here are some uses of constants:
. 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.

Expression containing constants
X=(2/3)*4
int z = 34

Here (2/3)*4 and 34 are the constant values.

Let us see a simple program on the constant expressions

Example:

#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 o;
}

#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 o;
}

Output:

Value of x is : 3

2.) Integral expressions: 

An integer express

sion is an expression that produces the integer value as output after performing all the explicit and implicit conversions.
An expression containing integrals:
(x*y)-7
Here x and y are integers.

Let us see a simple program on the integral expression

Example: 

#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;
}

#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:

See also  Operators | C++

Enter the value of x and y

7

9

value of z is: 16

3.) Float expressions:

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

Expressions containing float
X + float(10)
34.5

Let us see a simple program on the float expression:

#include<iostream>

using namespace std;
int main()
{
float x=9.8; //variable declaration.
float y=6.5; //variable declaration.
float z;
std::cout <<”value of z is:” <<z<<std::endl; //displaying the value of z.
return 0;
}

#include<iostream>
using namespace std;
int main()
{
float x=9.8; //variable declaration.
float y=6.5; //variable declaration.
float z;
std::cout <<”value of z is:” <<z<<std::endl; //displaying the value of z.
return 0;
}
 

Output:-
value of z is: 16.3

4.) Pointer expressions:

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

Expressions containing pointer

&x

ptr

ptr++

ptr-

let us see a simple program on pointer expression:

Example:

#include<iostream>

using namespace std;
int main()
{
int a[]={1,2,3,4,,5,6}; //array initialization
int*ptr; //pointer declaration
ptr=a; //assigning the base address of the array to the pointer ptr
ptr=ptr+1; //incrementing the value of pointer
std::cout<<”value of fourth element of an array:”<< *ptr<<std::endl;
return 0;
}

#include<iostream>
using namespace std;
int main()
{
 int a[]={1,2,3,4,,5,6}; //array initialization 
int*ptr; //pointer declaration
ptr=a; //assigning the base address of the array to the pointer ptr
ptr=ptr+1; //incrementing the value of pointer
std::cout<<”value of fourth element of an array:”<< *ptr<<std::endl;
return 0;
}

Output:

value of the fourth element of an array: 4.

5.) 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.

Expressions containing relation:

a>b

a-b>=x-y

let us see a simple program on relation expression.

Example:

#include<iostream>

                using namespace std;
                int main()
                {
                      int a=45; //variable declaration
                      int b=17; //variable declaration
                      bool y=a>b; //relational expression
                       cout<<”value of y is :”<<y; //displaying the value of y.

return 0;
}

#include<iostream>
                	using namespace std;
                	int main()
                	{
                  		int a=45; //variable declaration
                  		int b=17; //variable declaration
                  		bool y=a>b; //relational expression
                   		cout<<”value of y is :”<<y; //displaying the value of y.
return 0;
}

Output:

See also  UNORDERED MULTIMAP in Cpp

Value of y is: 1

6.) Logical expressions:

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

Expressions containing logic:

a>b && x>y  

a>10 || b==5  

Let us see a simple example of logical expression.

Example:

#include <iostream>  

using namespace std;  

int main() {  

int a=2;  

int b=7;  

int c=4;  

cout<<((a>b)||(a>c));  

return 0;  

}  

#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 that is used to manipulate the data at a bit level. They are 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.

In the above example, the value of ‘x’ is 3 and its binary value is 0011. We are shifting the value of ‘x’ by a three-bit position to the right. Let’s understand through the diagrammatic representation.

Let’s see 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 have declared a variable ‘x’. After declaration, we applied the bitwise operator, i.e., the right shift operator to shift the one-bit position to the right.

Output

2

Let’s look at another example.

1. #include <iostream>  

2. using namespace std;  

3. int main()  

4. {  

5.  int x=7; // variable declaration  

6. std::cout << (x<<3) << std::endl;  

7. return 0;  

8. }  

In the above code, we have declared a variable ‘x’. After declaration, we applied the left shift operator to variable ‘x’ to shift the three-bit position to the left.

Output

56

Special Assignment Expressions

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

Chained Assignment

A chained assignment expression is an expression in which the same value is assigned to more than one variable by using a single statement.

See also  Arrays in c++

For example:

1. a=b=20   

2. or   

3. (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 have declared two variables, i.e., ‘a’ and ‘b’. Then, we assigned the same value to both variables using a chained assignment expression.

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 an 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  

 std::cout <<“Values of ‘a’ is ” <<a<< std::endl;  

 return 0;  

}  

In the above code, we have declared two variables, i.e., ‘a’ and ‘b’. Then, we applied embedded assignment expression (a=10+(b=90)).

Output

Values of ‘a’ is 100 

Compound Assignment

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

For example,

1. 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; // displaying the value of a.  

  return 0;  

}  

In the above code, we have declared a variable a and assigned 10 values to this variable. Then, we applied compound assignment operator (+=) to ‘a’ variable, i.e., a+=10 which is equal to (a=a+10). This statement increments the value of a by 10.

Output

Value of a is :20 

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.