Answer

You might think that the C compiler would complain that ``='' isn't a relational operator, so it can't be used in an if condition. But in fact, this isn't what will happen.

It turns out that in C, you can use the ``='' assignment operator in all kinds of places-including inside the conditions for if statements! So if you put the statement:

      if (delta = 0.0) ...

in your program, what will happen is that the value 0.0 will be assigned to the variable delta. (And in this case, for reasons that we won't go into, the computer will go to the else part of the if statement.) The effect is obviously very different than if you had written:

      if (delta == 0.0) ...

which compares the value of delta to 0.0, and which does not change the value of delta!

Return to lesson.



Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah