[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem usning 'OR' operator in 'IF' condition?

dare ruby

2/14/2008 7:01:00 AM

Dear friends,

I have got an error when i tried to check multiple condition using "OR"
operator inside "IF" loop.

my code is

if @character != 'R' || @character != 'r'

raise " Expected 'R' after 'E' in version Declaration"

end

Even when i pass a value for @character as 'R' its showing error like

Expected 'R' after 'E' for version Declaration (RuntimeError)

Please could any one explain in detail about the problem and how to
solve it when i have to use multiple check in the same if loop.

Thanks in advance

Regards,
Martin
--
Posted via http://www.ruby-....

7 Answers

Paul McMahon

2/14/2008 7:20:00 AM

0

Apply De Morgan's and you'll see this condition is always true:

@character !=3D 'R' || @character !=3D 'r'

is equivalent to

!(@character =3D=3D 'R' && @character =3D=3D 'r')

As @character can never be both 'R' and 'r', @character =3D=3D 'R' && =

@character =3D=3D 'r' is always false, so the statement is

!(false)

which is true.

So your code is equivalent to

if true
raise " Expected 'R' after 'E' in version Declaration"
end

which is equivalent to

raise " Expected 'R' after 'E' in version Declaration"

You really mean something like

if @character !=3D 'R' && @character !=3D 'r'
raise " Expected 'R' after 'E' in version Declaration"
end

Which is better done

if @character !~ /^r$/i
raise " Expected 'R' after 'E' in version Declaration"
end

If you use regular expressions, then you can probably save yourself a lo=
t =

of unecessary conditionals by writing something like

if some_string !~ /^er$/i
raise " Expected 'R' after 'E' in version Declaration"
end

Mark Wickens

2/14/2008 7:22:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

'R' != 'r', so the OR is true. If fact, it will always be true. You want an
&& operator.

Mark

On Thu, Feb 14, 2008 at 2:01 AM, dare ruby <martin@angleritech.com> wrote:

> Dear friends,
>
> I have got an error when i tried to check multiple condition using "OR"
> operator inside "IF" loop.
>
> my code is
>
> if @character != 'R' || @character != 'r'
>
> raise " Expected 'R' after 'E' in version Declaration"
>
> end
>
> Even when i pass a value for @character as 'R' its showing error like
>
> Expected 'R' after 'E' for version Declaration (RuntimeError)
>
> Please could any one explain in detail about the problem and how to
> solve it when i have to use multiple check in the same if loop.
>
> Thanks in advance
>
> Regards,
> Martin
> --
> Posted via http://www.ruby-....
>
>

dare ruby

2/14/2008 8:50:00 AM

0

Thank you paul and mark your informations have been very useful to me.

but it works fine when the condition changes like,



if @character == 'R' || @character == 'r'

raise " Expected 'R' after 'E' in version Declaration"

end




Regards,
Martin
--
Posted via http://www.ruby-....

Robert Klemme

2/14/2008 1:36:00 PM

0

2008/2/14, dare ruby <martin@angleritech.com>:
> Thank you paul and mark your informations have been very useful to me.
>
> but it works fine when the condition changes like,
>
> if @character == 'R' || @character == 'r'
> raise " Expected 'R' after 'E' in version Declaration"
> end

Are you sure? From what I gather this logic is still flawed. I mean,
you want to throw if it is *neither* "r" *nor* "R". So you would need
to express that in your conditions, e.g.

if @char != 'R' && @char != 'r' ...
if ! ( @char == 'R' || @char == 'r' ) ...

(btw, see Morgan's Laws: http://en.wikipedia.org/wiki/De_Morgan&... )

Here's an alternative way

raise "..." unless /^r$/i =~ @character

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Todd Benson

2/14/2008 5:56:00 PM

0

On Thu, Feb 14, 2008 at 1:01 AM, dare ruby <martin@angleritech.com> wrote:
> Dear friends,
>
> I have got an error when i tried to check multiple condition using "OR"
> operator inside "IF" loop.
>
> my code is
>
> if @character != 'R' || @character != 'r'
>
> raise " Expected 'R' after 'E' in version Declaration"
>

The statement " If character is not 'R' _OR_ character is not 'r' "
will always be true. If it is "R", then it is not "r", and vice
versa.

In the other one that you said works (with the == instead of the !=)
contradicts a little with your original code. Do you want to raise
when there exists an [rR] or when there does not exist an [rR]?

Look at Robert's code. I like the one with the regular expression.

Todd

Sebastian Hungerecker

2/14/2008 10:19:00 PM

0

dare ruby wrote:
> but it works fine when the condition changes like,
>
> =C2=A0 =C2=A0 =C2=A0if @character =3D=3D 'R' =C2=A0|| @character =3D=3D '=
r'
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 raise " Expected 'R' after 'E' in version Dec=
laration"
> =C2=A0 =C2=A0 =C2=A0 end

Here you are saying: if character is 'R' or if it is 'r'. This condition=20
obviously can be true (if it is one of 'r' or 'R') or false (if it's=20
something else).
Previously you were saying: if character is something other than 'R' or it =
is=20
something other than 'r'. This will, as Paul pointed out, always be true.=20
Just think about: If character is 'R' than the first part won't be true ('R=
'=20
is not different from 'R'), but the second part will (because 'R' is=20
different from 'r'). If it is 'r', it's the other way around. And since the=
=20
whole expression is true when (at least) one of the parts is true (that's=20
what "or" means), it's always true. What you want is: !(@c =3D=3D 'R' || @c=
=3D=3D'r')=20
which is equivalent @c !=3D 'R' && @c !=3D 'r'. Note how here you have && i=
nstead=20
of ||. That's the law of DeMorgan that Paul mentioned:
!(a && b) <-> !a || !b
!(a || b) <-> !a && !b


HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

J. Cooper

2/15/2008 8:07:00 AM

0

dare ruby wrote:
> Thank you paul and mark your informations have been very useful to me.
>
> but it works fine when the condition changes like,
> if @character == 'R' || @character == 'r'
>
> raise " Expected 'R' after 'E' in version Declaration"
>
> end
>
> Regards,
> Martin

Yep. When you change == to !=, you have to flip your ||s to &&s to get
the same effect.

"If the light is green OR the light is blue, the world is in danger"
becomes
"If the light is NOT green AND the light is NOT blue, the world is NOT
in danger"

P.S. Just use what Robert said unless regular expressions scare you. Or
do the whole
raise "Expected 'R' after 'E' in version declaration" unless
@character.downcase == 'r'
--
Posted via http://www.ruby-....