Tylv Tylv - 8 months ago 41
C++ Question

When is it appropriate to use = or == in C++?

What is the difference between the = and == operator in C++?

As this is a new language I am coding in I forgot the basics of coding operators such as = and == Ignore this question, I have already flagged it, I just got confused going into a new language that is all, Please vote to delete the question


One is assignment and the other is comparing for equality. They do different things, so are "appropriate" in different contexts.

When you do

if (UI = 'y'){...}

you assign 'y' to the variable UI, and then that value is used as the boolean expression. The character 'y' is non-zero, which in C++ is the same as true.

When you do

if (UI == 'y'){...}

you compare the value in the variable UI with the value 'y' for equality. If both are equal the result is true otherwise it's false.