Difference between = and == ? EasyC

When the assignment block “lastcommand = 0” written as “lastcommand == 0” it only executes “else if ( lastcommand == 0)” no matter what.

Meanwhile if written with just one equal sign it works.


This brings me to ask what is the difference between the single and double equal signs and when should I use either?

I am not sure what is happening in the code, but I do know that = will store a value. So LastCommand = 0 will set LastCommand to 0. Meanwhile, a == will test for equality. So LastCommand ==0 will test to see if LastCommand is equal to 0 or not. If it is, true is returned, if not, false is returned. Use = for setting variables, and use == in if statements, while statements, etc.

For example:
LastCommand = 0;
if(LastCommand ==0)
//return “yay”

will return yay:D

Okay I see now, thanks!

I know this was already addressed, but I want to chime in.

“=” is the assignment operator in C. The left value or lvalue is the destination for whatever is on the right, or the rvalue.

So int foo = 5; sets foo to 5.

“==” is the comparison operator. It compares the lvalue to the rvalue. It will output either true in the form of a 1 or false in the form of a 0.

So (5 == 7) is false which is 0, (5 == 5) is true which is 0.

Always use “==” for comparisons, and always use “=” for assignments.

Corrected text is:
So (5 == 7) is false which is 0, (5 == 5) is true which is 1.

Sometimes Cody’s fingers can’t keep up :rolleyes:

I thought I might chime in with the opposite as it can be quite useful sometimes.

The ‘opposite’ of == is != Which means is not equal.

1 != 2 would return 1 (or true) because 1 is not equal to 2,
1 != 1 wound return 0 (or false) because 1 is equal to 1


+1 :slight_smile:

This is our 4th year using RobotC and this is the first time I really understood the difference between = and ==. The != makes a lot of sense now as well. Thank you all!

I have found this to be true also

Whoops. Copy and paste got me. Not the first nor the last time that’ll happen.

I tend to do insane multitasking in class. Pretty sure I wrote this in Advanced Programming a course that bores me to tears. I am usually switching between email, whatever homework I happen to be doing, sort of listening to the “lecture” and doing whatever else seems like it’ll make the deathgrip and migraine that comes with having to watch some professor who barely understands how to communicate with human beings try to explain concepts he/she (she in this case) barely understands.

That’s why a class called Advanced is in actuality an Intro to Java course. Something I don’t need and am thoroughly … angry … my department is making me, scratch that, forcing me to endure.

But yeah, back to screwing up the most simple boolean algebra ever!

Now now, no need to be redundant Cody. You already said professor.:wink:

As a university professor, I can tell you how these classes get named:

(Prog 101) Introduction to programming - The microwave. Class will cover all aspects of programming: Standard time lengths, use of delayed start, management of power levels, defrost cycles. More advanced topics like the “Popcorn” and “Pizza” buttons will be covered in a special Saturday Seminar that is open to the public.

(Prog 102) Introduction to programming - The VCR. (Prereq Prog 101) This class will cover programming the home VCR. Initial setup of the time and date, cable / antenna, daily, weekly and special event programming and the use of “plus time” to capture sporting events of variable length. Note: Discussions around VHS vs Beta vs Optical Disk will be covered in a joint session with Tech 102.

(Prog 201) Advanced Programming: Java. This class will cover all aspects of programming modern coffee equipment from simple K-cup to more complex cafe-mocha-steamed-extra shots-no whipped cream factory devices. Programming bean roasters, grinders and air tight packing machines is also covered.

(Prog 202) Advanced Programming: Introduction to Java. Programming the modern computer will be covered …

See, it really is an advanced class :rolleyes: Tough it out, get the easy A. Tutor the students that don’t get it for some extra cash or free pizza.

At least you don’t have to endure half a year of nicely formatting your excel documents. Compulsory in my year, a whole hour 5 times a week of turning images of excel documents into excel documents. Then listening to our teacher explain the average function for the one-quadrillionth time :stuck_out_tongue:

Sorry forgot my DRY methodology. Nice one.

Yikes. What class is that, Advanced Statistical Programming Operations?