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

D

dare ruby

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
 
P

Paul McMahon

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
 
M

Mark Wickens

[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
 
D

dare ruby

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
 
R

Robert Klemme

2008/2/14 said:
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's_laws )

Here's an alternative way

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

Kind regards

robert
 
T

Todd Benson

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
 
S

Sebastian Hungerecker

dare said:
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: (e-mail address removed)
ICQ: 205544826
 
J

J. Cooper

dare said:
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'
 

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
474,283
Messages
2,571,405
Members
48,100
Latest member
Calfin5299

Latest Threads

Top