Difference between assignment operator and equality operator in c++ code


Assignment Operator:

Assignment operator is used to assign the value of one variable of another  variable. = sign is show the assignment operator. Assignment operator is used in every where. You can watch the code to understand the assignment operator.

Code:


#include <iostream>
#include <string>
using namespace std;
int main() {
    int num1,num2;
    cout<<"Enter a number : ";
    cin>>num2;
    num1=num2;
    cout<<"Num1 : "<<num1<<endl;
    cout<<"Num2 : "<<num2;
}

Output:


Enter a number : 13
Num1 : 13
Num2 : 13
You can see the output and understand the use of assignment operator.

Equality Operator:


Equality operator is used as a condition. We can say that if the number is equal to that number in the condition than you enter the that statement. Equality operator is mostly used in if and else statement. == sign is used for equality operator. You can see the code to understand the equality operator easily.

Code:


#include <iostream>
#include <string>
using namespace std;
int main() {
    int num;
    cout<<"Enter a number : ";
    cin>>num;
    if(num==20)
    {
                cout<<"How are you. ";
                }
                else if(num==30)
                {
                                cout<<"Hello Friends. ";
                }
                else
                {
                                cout<<"Fine ";
                }
}

Output:


When enter 20
Enter a number : 20
How are you.

When enter 30
Enter a number : 30
Hello Friends.

When enter 38
Enter a number : 38
Fine

Hope you understand the difference between assignment operator and equality operator.


Click here for watch the video of that code



avatar
AUTHOR: Muhammad Abbas
I am Software Engineer.

0 Comments