You may wish to compare two operands to see if they are equal. C provides an equality operator for this purpose. We are now approaching a big problem area with C. You might think that the equality operator is used as follows:

count = 99

In some languages this is exactly how I would compare to items. However in C it is not. We have already seen that the = operator is used to assign an expression to a variable, i.e. the result of the above statement would be to put 99 into the variable counter. This is not what you want to do if you are just comparing counter and a limit value.

Instead the equality operator in C is the character pair == You put the two equals characters right next to each other:

count == 99

If I want to test to see if two values are different I can put:

count != 99

The character pair != is the "not equals" operator.

One of the truly nasty things about C is that many compilers will perform the assignment when you really want to do an equality test. In some rare cases this can be used to speed up and shorten your programs, in that you can do two things at the same time. However, it is more often just going to cause problems for you. For this reason the compiler which we are using will refuse to perform the assignment in a test (i.e. it is clever enough to decide that what you are doing is silly and will refuse to compile such stupidity).

When you run the program on the right, the first condition tests to see if count is equal to 100, this will fail because we can see that count is only 99. However if we add 1 to the value in count and try the test again we find that it succeeds, and the program will execute the block which is attached to the if statement. We can now make our programs change what they do in response to the actions of the user. You will see and if construction used in the first example programs, where we turn a light on and off depending on whether the user has pressed a button.