WHEN DO WE USE SINGLE = SIGN AND WHEN DO WE USE 2 == SIGNS AS SUCH?

In most cases we use single = sign but here inside the round brackets of the if block 2 == signs have been used. Please explain when we use which ?

Hey @gopsss12

= is an assignment operator and u can say
if LHS=RHS then here LHS value is set to RHS

== is a conditional operator and if LHS==RHS then it returns true if both LHS & RHS are equal otherwise false

I hope this resolves your query :slight_smile:

But where do we use == and where do we use = sign can you please explain with examples??Will = work inside circular brackets?

Yes it will work there as well but we dont use it there

int a=10 ; //here we want to assign value hence single =

if(a==10) //here we want to check if a is equal to 10 or not hence ==

Here refer to this as well

Assignment Operator (=)

= is an Assignment Operator in C, C++ and other programming languages, It is Binary Operator which operates on two operands.

= assigns the value of right side expression’s or variable’s value to the left side variable.

Let’s understand by example:

x=(a+b);
y=x;
Here, When first expression evaluates value of (a+b) will be assigned into x and in second expression y=x; value of variable xwill be assigned into y.

Equal To Operator (==)

== is an Equal To Operator in C and C++ only, It is Binary Operator which operates on two operands.

== compares value of left and side expressions, return 1 if they are equal other will it will return 0.

Let’s understand by example:

int x,y;
x=10;
y=10;
if(x==y)
printf(“True”);
else
printf(“False”);
When expression x==y evaluates, it will return 1 (it means condition is TRUE) and “TRUE” will print

If your doubt is resolved then hit a like to let me know :slight_smile:

1 Like

In the above example you gave and in general if we use = instead of == in normal brackets of IF ELSE CONDITION will it work or throw an error I presume it will throw an error BECAUSE THEN WE ARE ASSIGNING A VALUE TO THE VARIABLE IN QUESTION AND NOT COMPARING ITS VALUE TO SOMETHING ELSE but can you please confirm the same??

No it wont throw an error
Im that case it will assign x with y and that condition will be evaluated as true always

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.