Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problems with logic operations within loops

Reply
Thread Tools

problems with logic operations within loops

 
 
Eric Sosman
Guest
Posts: n/a
 
      01-30-2010
On 1/30/2010 9:40 AM, Tim Streater wrote:
> On 30/01/2010 13:28, Ben Bacarisse wrote:
>> Tim Streater<> writes:
>> <snip>
>>> And I prefer to write "==true" because it makes my code more readable
>>> to me.

>>
>> You are probably still talking about PHP, but just in case this gets
>> taken as a more general point, it is unwise to do that in C because
>> true must be just one value and any non-zero values is acceptable as
>> "not false" (such as those that the isxxxx functions might return).
>>
>>> 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?

>>
>> I'd say that is the fault of whoever chose x as the name. If x is a
>> boolean, the name should indicate WTF: if (x_needs_hair_cut)... If x
>> is not boolean (say it is the number of until a hair cut is required)
>> I would test against 0: if (x_days_to_hair_cut == 0)...

>
> 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.

--
Eric Sosman
lid
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      01-30-2010
Eric Sosman <> writes:
> 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.

 
Reply With Quote
 
 
 
 
achp
Guest
Posts: n/a
 
      01-30-2010
On Jan 30, 1:18*pm, Tim Streater <timstrea...@waitrose.com> wrote:

> 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...
 
Reply With Quote
 
achp
Guest
Posts: n/a
 
      01-30-2010
On Jan 30, 3:19*am, Barry Schwarz <schwar...@yahoo.com> wrote:

> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-30-2010
(Stefan Ram) writes:
> Eric Sosman <> writes:
>> 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.


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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-30-2010
On 2010-01-30, Stefan Ram <> wrote:
> Eric Sosman <> writes:
>> 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.


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
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      01-30-2010
Keith Thompson <kst-> writes:
> Daniel Giaimo <> writes:
>> On 1/29/2010 3:12 AM, Nick Keighley wrote:
>>> On 28 Jan, 11:07, Phil Carmody<thefatphil_demun...@yahoo.co.uk>
>>> wrote:
>>>> Tim Streater<timstrea...@waitrose.com> writes:
>>>
>>>>> if (isspace(ch)==true) continue;
>>>>
>>>> I don't remember seeing anyone jump on that line. isspace() returns
>>>> something which is either zero or non-zero. Do not pretend you can
>>>> guess what non-zero value it might have. (And cast not-unsigned
>>>> chars to unsigned char before passing them to it, of course.)
>>>
>>> there's an easy work around just define "true" as non-zero!
>>>
>>> #define true !0

>>
>> I don't know if you're joking, but that won't work. !0 is 1
>> not any value other than 0, so, for example, 2==!0 is false.

>
> 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
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Logic Map/Logic Flow Chart. (Example Provided) spike Python 8 02-09-2010 12:31 PM
Asynchronous Logic Gates and Analog Logic Gates Jyoti Ballabh Software 3 11-26-2009 06:48 PM
Successive arithmetic operations within a process dennismccarty VHDL 1 03-15-2009 08:20 AM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57