Problem with ()?():() as ending test in for-loop

Tom Karzes karzes at mfci.UUCP
Sat May 27 05:16:30 AEST 1989


In article <779 at attila.WEITEK.COM> kens at attila.WEITEK.COM (Ken Stanley) writes:
}The specific example: "((n % 10) != 0 && k > 2) || n < 100" works because
}when n = 100, (n % 10) == 0 and ! (n < 100).  However, if the loop increment
}were 3 instead of 1, it would not.
}...
}... these two statements are identical:
}
}for (n = 1; ((n % 10) != 0 && k > 2) || n < 100; n++)
}for (n = 1; ((n % 10) ? (k > 2) : (n < 100); n++)

No, these two statements are not identical, even in this context.  Consider
the case where n is 11 and k is 1.  The original ?: expression evaluates
to 0, whereas the incorrect alternate form evaluates to 1.  (By the way,
the second expression shown above has unbalanced parentheses.)



More information about the Comp.lang.c mailing list