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