Monday, February 9, 2009

This Is A Trick, Be Careful

The last example of the third group contains a bit of a trick, but since we have covered it above, it is nothing new to you. Notice that the first part of the compare evaluates to "FALSE". The remaining parts of the compare are not evaluated, because it is an "AND" and it will definitely be resolved as a "FALSE" because the first term is false. If the program was dependent on the value of "y" being set to 3 in the next part of the compare, it will fail because evaluation will cease following the "FALSE" found in the first term. Likewise, "z" will not be set to 4, and the variable "r" will not be changed.

Potential Problem Areas


The last group of compares illustrate three possibilities for getting into a bit of trouble. All three have the common result that "z" will not get set to the desired value, but for different reasons. In the case of the first one, the compare evaluates as "true", but the semicolon following the second parentheses terminates the "if" clause, and the assignment statement involving "z" is always executed as the next statement. The "if" therefore has no effect because of the misplaced
semicolon. The second statement is much more straight forward because "x" will always be equal to itself, therefore the inequality will never be true, and the entire statement will never do a thing, but is wasted effort. The last statement will always assign 0 to "x" and the compare will therefore always be "false", never executing the conditional part of the "if" statement.
The conditional statement is extremely important and must be thoroughly understood to write efficient C programs. If any part of this discussion is unclear in your mind, restudy it until you are confident that you understand it thoroughly before proceeding onwar

Google Search

Custom Search