problems with logic operations within loops

E

Eric Sosman

Could do that I suppose, but "if (debugflag_is_true) { ..." gets tedious
quite quickly.

You'd prefer `if (debugflag_is_true == true)', I guess? ;-)

There's also the question of when to stop:

if (x)
if (x == true)
if (x == true == true)
if (x == true == true == true)
...

It's mostly a matter of personal style, but I've never
found a convincing reason (in any language) to compare with a
Boolean constant. I like explicit comparisons of pointers
to NULL and numbers to zero: `if (ptr != NULL)' rather than
`if (ptr)', but it's just a preference. (I particularly abhor
`if (!strcmp(answer,"no"))', which reads so wrongly it rouses
righteous rage.)

Also, as mentioned up-thread, `if (isdigit(ch) == true)'
is just plain wrong, R-O-N-G, wrong.
 
S

Stefan Ram

Eric Sosman said:
There's also the question of when to stop:
if (x)
if (x == true)
if (x == true == true)
if (x == true == true == true)
...

He wants to stop at the second case, because this is
readable as English »if x is true, then«, while neither
»if x, then« nor »if x is true is true, then« is English.
 
A

achp

And I prefer to write "==true" because it makes my code more readable to
me. If I have "if (x>3) { ..." I can read this as "if x is greater then
3 then do so-and-so". I read "if (x) { ..." as "if x then do so-and-so".
WTF? If x *what*? Forgot to put the cat out? Needs its hair cut?

Well, in this case it's perfectly transparent: "if x is space
then...".

Or at least it would be so if not that disgusting flaw with the is*
functions...
 
A

achp

2 - Your if expression evaluates to true under all defined
conditions.  Probably not what you had in mind.

I'd also like to add why.

In Boolean algebra, there are so called De Morgan's laws. In terms of
C,

!(X && Y) == (!X) || (!Y)

and

!(X || Y) == (!X) && (!Y)

Therefore, when you swap "then" and "else" branches of an "if"
statement, you need to not only negate the condition clauses, but also
to replace all conjunctions into disjunctions and vice versa.
 
K

Keith Thompson

He wants to stop at the second case, because this is
readable as English »if x is true, then«, while neither
»if x, then« nor »if x is true is true, then« is English.

Only because "x" is a poor name. If it were named, say, "done",
would you consider writing
if (done == true)
rather than
if (done)

BTW, C99 does have "true" and "false", if you #include <stdbool.h>,
and you can easily declare them in C90 if you like.
 
S

Seebs

He wants to stop at the second case, because this is
readable as English »if x is true, then«, while neither
»if x, then« nor »if x is true is true, then« is English.

Sure they are. Especially if x is already a predicate:

if molly is a kitty
if "molly is a kitty" is true
if ""molly is a kitty" is true" is true

-s
p.s.: molly is in fact a kitty
 
P

Phil Carmody

Keith Thompson said:
Apparently you missed (and snipped) the smiley.

It was clear you forgot that true has to be _any_ non-zero. So you want:

#define true .ne. !0
....
int condition=get_condition();
if (condition true) { ...


Muahahahah....

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top