twitter
    Find out what I'm doing, Follow Me :)

More Compares

The compares in the second group are a bit more involved. Starting with the first compare, we find a rather strange looking set of conditions in the parentheses. To understand this we must understand just what a "true" or "false" is in the C language. A "false" is defined as a value of zero, and "true" is defined as a non-zero value. Any integer or char type of variable can be used for the result of a true/false test, or the result can be an implied integer or char. Look at the first compare of the second group of compare statements. The expression "r is != s" will evaluate as a "true" since "r" was set to 0.0 above, so the result will be a non-zero value, probably 1. Even though the two variables that are compared are "float" variables, the result will be of type "integer". There is no explicit variable to which it will be assigned so the result of the compare is an implied integer. Finally the resulting number, 1 in this case, is assigned to the integer variable "x". If double equal signs were used, the phantom value, namely 1, would be compared to the value of "x", but since the single equal sign is used, the value 1 is simply assigned to "x", as though the statement were not in parentheses. Finally, since the result of the assignment in the parentheses was non-zero, the entire expression is evaluated as "true", and "z" is assigned the value of 1000. Thus we accomplished two things in this statement, we assigned "x" a new value, probably 1, and we assigned "z" the value 1000. We covered a lot in this statement so you may wish to review it before going on. The important things to remember are the values that define "true" and "false", and the fact that several things can be assigned in a conditional statement. The value assigned to "x" was probably a 1 but different compilers may assign a different value as long as it is non-zero.

The next example should help clear up some of the above in your mind. In this example, "x" is assigned the value of "y", and since the result is 11, the condition is non-zero, which is true, and the variable "z" is therefore assigned 222. The third example, in the second group, compares "x" to zero. If the result is true, meaning that
if "x" is not zero, then "z" is assigned the value of 333, which it will be. The last example in this group illustrates the same concept, since the result will be true if "x" is non-zero. The compare to zero is not actually needed and the result of the compare is true. The third and fourth examples of this group are therefore identical.