C++ expression consists of operators, constants, and variables which are organized according to the rules of the language. It can likewise contain function calls which return values. An expression can consist of at least one operands, at least zero operators to compute a worth. Each expression produces some worth which is allocated to the variable with the help of an assignment operator.
Examples of expression in C++ :
(a+b) - c(x/y) -z
4a2 - 5b +c
(a+b) * (x+y)
An expression in c++ language can be of following types:
- Constant expressions
- Integral expressions
- Float expressions
- Pointer expressions
- Relational expressions
- Logical expressions
- Bitwise expressions
- Special assignment expressions
In the event that the expression is a blend of the above expressions, such expressions are known as compound expressions.
Constant expressions in c++
A constant expression is an articulation that consists of just constant worths. It is an expression whose value is determined at the compile-time but assessed at the run-time. It very well may be made out of integer, character, floating-point, and enumeration constants.
Constants are utilized in the accompanying situations:
- It is utilized in the subscript declarator to describe the array bound.
- It is utilized after the case keyword in the switch statement.
- It is utilized as a numeric worth in an enum
- It determines a bit-field width.
- It is utilized in the pre-processor #if
In the above situations, the constant articulation can have integer, character, and enumeration constants. We can utilize the static and extern keyword with the constants to characterizes the function-scope.
The following table shows the expression containing constant worth:
Expression containing constant | Constant value |
---|---|
x = (2/3) * 4 | (2/3) * 4 |
extern int y = 67 | 67 |
int z = 43 | 43 |
static int a = 56 | 56 |
We should see a basic 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;
}
In the above code, we have first declared the 'x' variable of integer type. After declaration, we assign the simple constant expression to the 'x' variable.
Output:-
Integral Expressions in C++ Language:-
An integer articulation is an expression that delivers the integer worth as result after performing all the explicit and certain.
Accompanying are the instances of integral expression:
(x * y) -5
x + int(9.0)
where x and y are the Integer number.
We should see a basic instance 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; }
In the above code, we have declared three variables, i.e., x, y, and z. After declaration, we take the user input for the values of 'x' and 'y'. Then, we add the values of 'x' and 'y' and stores their result in 'z' variable.
Output
Enter the values of x and y 8 9Value of z is :17
We should 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; }
In the above code, we proclaim two variables, i.e., x and y. We store the worth of expression (y+int(10.0)) in a 'x' variable.
Output
Value of x : 19Float Expressions in C++ Programming:-
A float articulation is an expression that delivers floating-point worth as result after performing all the explicit and certain.
The accompanying are the examples of float articulation:
x+y
(x/10) + y
34.5
x+float(10)
Let's understand through an instance.
#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; std::cout <<"value of z is :"<< z <<std::endl; // displaying the value of z. return 0; }
Output
We should see another instance 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 std::cout <<"value of y is :" <<y<<std::endl; // displaying the value of y return 0; }
In the above code, we have proclaim two variables, i.e., x and y. After declaration, we store the worth of articulation (x+float(10)) in variable 'y'.
Output
value of y is :16.7Pointer Expressions in C++ Language:-
A pointer articulation is an expression that delivers address worth as an result.
The accompanying are the instances of pointer expression:
ptr ptr++
ptr-
We should 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 std::cout <<"value of second element of an array : " << *ptr<<std::endl; return 0; }
In the above code, we announce the array and a pointer ptr. We allot the base location to the variable 'ptr'. After assigning the address, we increment the worth of pointer 'ptr'. Whenever pointer is incremented then 'ptr' will point the second component of the array.
Output
value of second element of an array : 2Relational Expressions in C++
A relational articulation is an expression that delivers a worth of type bool, which can be either true or false. It is otherwise called a boolean expression. At the point arithmetic expressions are utilized on both sides of the relational operator, arithmetic expressions are assessed first, and then their outcomes are analyzed.
The accompanying are the instances of the relational expression:
a>b
a-b >= x-y
a+b>80
Let's understand through an instance
#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; }
In the above code, we have proclaimed two variables, i.e., 'a' and 'b'. After declaration, we have applied the relational operator between the variables to check whether 'a' is greater than 'b' or not.
Output
Value of y is :0We should 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; }
In the above code, we have announced four variables, i.e., 'a', 'b', 'x' and 'y'. Then, we apply the relational operator (>=) between these factors.
Output
1Logical Expressions in C++ Language:-
A logical expression is an expression that joins at least two relational expressions and produces a bool type value. The logical operators are '&&' and '||' that joins at least two relational expressions.
The accompanying are few examples of logical expressions:
a>b && x>y
a>10 || b==5
We should see a basic instance 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
0Bitwise Expressions in C++ :-
A bitwise expression is an articulation which is utilized to manipulate the data at a bit level. They are basically utilized to shift the bits.
For example:
x=3
x>>3 // This statement means that we are shifting the three-bit step to the right.
In the above instance, the worth of 'x' is 3 and its binary worth is 0011. We are shifting the worth of 'x' by three-bit position to the right.
Let's see a basic 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 announced a variable 'x'. After declaration, we applied the bitwise administrate, i.e., right shift operator to shift one-bit position to right.
Output
2Let's look at another instance.
#include <iostream> using namespace std; int main() { int x=7; // variable declaration std::cout <<(x<<3) <<std::endl; return 0; }
In the above code, we have proclaimed a variable 'x'. After declaration, we applied the left shift administrator to variable 'x' to shift the three-bit position to the left.
Output
56
Special Assignment Expressions in C++ Language:-
Special assignment expressions are the articulation which can be additionally ordered relying on the worth assigned to the variable.
Chained Assignment in C++ :-
Chained assignment expression is an expression where a similar value is assigned to more than one variable by utilizing single statement.
For example:
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 std::cout <<"Values of 'a' and 'b' are : "<<a <<","<<b<< std::endl; return 0; }
In the above code, we have announced two factors, i.e., 'a' and 'b'. Then, we have assigned the same worth to both the variables utilizing chained assignment articulation.
Output
Values of 'a' and 'b' are : 80,80Embedded Assignment Expression in C++ :-
An embedded assignment expression is an assignment articulation in which assignment expression is enclosed inside another assignment expression.
Let's understand through an instance.
#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 announced two factors, i.e., 'a' and 'b'. Then, we applied embedded assignment articulation (a=10+(b=90)).
Output
Values of 'a' is 100Compound Assignment Expression:-
A compound assignment expression is an articulation which is a combination of an assignment administrator and binary administrator.
For example,
In the above statement, 'a' is a variable and '+=' is a compound statement.
We should 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; }
0 Comments
🙏🙏please don't enter any spam links on the comment box.....