why does false || false = true ???

why does false || false = true ???

Post by Randall W. Hr » Sat, 26 Jun 1993 08:16:36



Here's one that's confusing me...  While solving a problem in
a programing book I came up with this, but I don't understand
why it works.  (I just kept trying things until I got the right
answer)

i=1
while [ $(($i%3)) -ne 2 ] || [ $(($i%5)) -ne 3 ] || [ $(($i%7)) -ne 4 ]
do
        let i=$i+1
done
echo The Number is ${i}.

The part that is confusing to me is the while condition.
I am "OR"ing 3 test conditions.  When they are all false,
the while loop is exited.  

It seems to work like,
when [ false OR false OR false ]
then
        exit loop
done

I'd think it would work like this,
when [ true AND true AND true ]
then
        exit loop
done

Here is the last 10 lines of a trace on the loop:

+ let i=50+1
+ [ 0 -ne 2 ]
+ let i=51+1
+ [ 1 -ne 2 ]
+ let i=52+1
+ [ 2 -ne 2 ]                   # false condition
+ [ 3 -ne 3 ]                   # false condition
+ [ 4 -ne 4 ]                   # false condition
+ echo The Number is 53.
The Number is 53.

Can anyone explain why OR'ing false statements works this way?

p.s. this isn't for a class or anything, so I don't have a
teacher to ask.

p.p.s.
Here is the original problem:
Write a program to find the smallest positive integer n
which corresponds to the following conditions:
n / 3 = integer x and remainder 2
n / 5 = integer y and remainder 3
n / 3 = integer z and remainder 4

--
Randall W. Hron
Roswell, GA

uunet!gatech!kd4nc!vdbsan!willard!hrandoz!hronr

 
 
 

why does false || false = true ???

Post by Michael Co » Sun, 27 Jun 1993 02:29:36



Quote:>Here's one that's confusing me...  While solving a problem in a programing
>book I came up with this, but I don't understand why it works.  (I just kept
>trying things until I got the right answer)
>i=1
>while [ $(($i%3)) -ne 2 ] || [ $(($i%5)) -ne 3 ] || [ $(($i%7)) -ne 4 ]
>do
>    let i=$i+1
>done
>echo The Number is ${i}.
>The part that is confusing to me is the while condition.  I am "OR"ing 3 test
>conditions.  When they are all false, the while loop is exited.
>It seems to work like,
>when [ false OR false OR false ]
>then
>    exit loop
>done

No, it's like this:

    when [ false AND false AND false ]
    then
        exit loop
    done

That's because of simple Boolean algebra:

    NOT(a OR b) = (NOT a) AND (NOT b).

Your `when' statement uses this incorrect rule:

    NOT(a OR b) = (NOT a) OR (NOT b)

Michael.

 
 
 

why does false || false = true ???

Post by ShadowMast » Sun, 27 Jun 1993 06:44:46



Quote:>Can anyone explain why OR'ing false statements works this way?
>Here is the original problem:
>Write a program to find the smallest positive integer n
>which corresponds to the following conditions:
>n / 3 = integer x and remainder 2
>n / 5 = integer y and remainder 3
>n / 3 = integer z and remainder 4

Let me save you some time right from the beginning!

if this is the original problem, then your program will run forever
because there is no solution!

the way I see it we can rephrase your problem like
so

n = 3x+2
n = 5y+4
n = 3z+4

which means 3x+2 = 3z+4

rearranging gives 3x = 3z + 2
dividing by 3 gives x = z +2/3

You tell me which integer x equals another integer z + 2/3

Besides this is nothing more than a simultaneous equation problem and does
not require a loop to begin with!  Solve the equations in terms of one
variable and choose 1 as you lowest integer for that variable and figure
out what the other variable would be.

Joe Carl

You must have misquoted the problem
--
ShadowMaster "If you ever drop your watch in a pool of molten lava, let it go,
              'cause man it's gone"          Saturday Nigh Live "Deep Thoughts"

 
 
 

why does false || false = true ???

Post by Robert Hartm » Sun, 27 Jun 1993 08:57:06



Quote:

>Here's one that's confusing me...  While solving a problem in
>a programing book I came up with this, but I don't understand
>why it works.  (I just kept trying things until I got the right
>answer)

>i=1
>while [ $(($i%3)) -ne 2 ] || [ $(($i%5)) -ne 3 ] || [ $(($i%7)) -ne 4 ]
>do
>    let i=$i+1
>done
>echo The Number is ${i}.

>The part that is confusing to me is the while condition.
>I am "OR"ing 3 test conditions.  When they are all false,
>the while loop is exited.  

No you're not.  You're executing the first test, and if it returns
false then you're executing the second, which if false executes the
third.  If you get to the third, then its outcome determines when
you exit the loop.

|| isn't a boolean arithmetic operator, it is a conditional execution
operator.  It executes the antecedent only when the precedent returns a
false exit code.

-r

 
 
 

why does false || false = true ???

Post by Robert Hartm » Sun, 27 Jun 1993 11:14:40



>|| isn't a boolean arithmetic operator, it is a conditional execution
>operator.  It executes the antecedent only when the precedent returns a

                            ^^^^^^^^^^

Quote:>false exit code.

Whoops!  Make that "conclusion."

-r

 
 
 

1. Help with False ${FALSE}

 In the stupid /usr/ports/editors/staroffice52/Makefile there is
a section which goes:
=====================
.if defined(WITH_CDROM)
.if !exists(${CD_MOUNTPT}/linux/office52/setup)





.endif
.endif
======================
 Despite the fact that when I do a
"ls -l /cdrom/linux/office52/setup" I get
-r-xr-xr-x  1 rot wheel 66936 May 12  2000 /cdrom/linux/office52/setup

 The Makefile is still saying there is no setup file!
 Can anyone suggest why?

----------
A nations human decency can be measured by the
number of its citizens who phone or write their government
to improve the world.
http://www.fandom.net/~daeron/Cyclo/Brainia/8635.html
http://www.topica.com/lists/WestPapua/read
http://www.foreignpolicy-infocus.org/briefs/vol5/v5n37papua.html

2. HELP I CAN'T LOGIN AS ROOT !

3. Linux BUGS? True or False!

4. Posting to correct group

5. blocking signals in signal handler -- True or False quiz

6. Oxygen VX1 AGP graphics car question.

7. Builtin commands true/false extension

8. Nic config and vtoc on linux

9. Multiple Sendmail Hosts: True or False?

10. true or False???

11. newbie testing true or false with the Dummies book

12. portable solution of /bin/sh true-false binary flag?

13. Chess is a game for 2 players - true or false?