Thursday, 9 July 2015

Difference Between One equal sign And Two Equals in Programming

we use two equal signs(==) to Compare two values as we do with one equal sign in Mathematics And if we use one Equal sign it shall Assign the value to the variable.
To understand that compare the given program below

Program with one Equal..

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Plz Enter a Number ";
    cin>>x;
  x=25;
    cout<<"The value of X is "<<x;
getch();
}

OUTPUT:

When We use one Equal then it Assigns Value to X and shows The Answer equal 25 regardless of what ever Value User enter in it.

Program with = =..

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Plz Enter a Number ";
    cin>>x;
    if(x==1)                                //  we use  
{
    cout<<"X is equal to 1 ";
}
else
{
    cout<<"Value of x is Unknown ";
}
getch();
}

OUTPUT

IF User in put 1 then it shall show the massage that "X is equal to 1 ";
Otherwise it shall show massage that "Value of x is Unknown ";

No comments:

Post a Comment