Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Help with while condition OR condition

Reply
Thread Tools

Help with while condition OR condition

 
 
John Feminella
Guest
Posts: n/a
 
      05-08-2011
> Everyone should use 'and' and 'or' by default instead of && and ||.
> Code reads better that way. =C2=A0Only if you have a specific reason to,
> should you use && or ||.


It's not a good idea to make a blanket rule like that, imo. The "and"
keyword is not really a substitute for "&&", since it has different
precedence. It's best viewed as a control flow modifier (like "if" or
"unless" when at the end of an expression), rather than a true logical
operator.

If you don't know that it's not quite the same, this can get you into
big trouble by leading to subtle bugs. Consider this code, for
instance:

>> missiles_armed =3D true

=3D> true
>> go_for_launch =3D false

=3D> false

# Using &&
>> go_for_launch && missiles_armed ? :fire_ze_missiles : :abort_launch

=3D> :abort_launch # Looks good here.

# Using "and"
>> go_for_launch and missiles_armed ? :fire_ze_missiles : :abort_launch

=3D> false # Uh-oh! We didn't get the `:abort_launch` we were expecting...

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Sat, May 7, 2011 at 22:29, 7stud -- <> wrote:
> Hi,
>
> A lot of beginners make the same mistake you did. =C2=A0 'Compound
> conditionals' have to be written like separate conditionals and then
> hooked together with an 'or' or 'and'. =C2=A0For instance if you wanted t=

o do
> something only if a number were greater than 5 and less then 10, you
> would do this:
>
> x > 5
> x < 10
> and
>
> if x > 5 and x < 10
> =C2=A0#do something
> end
>
> Everyone should use 'and' and 'or' by default instead of && and ||.
> Code reads better that way. =C2=A0Only if you have a specific reason to,
> should you use && or ||.
>
> Good luck.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      05-08-2011
7stud -- wrote in post #997311:
> In ruby, everyone should use 'and' and 'or' by default instead of && and
> ||.


I would advise exactly the opposite: there are many traps for the unwary
if you use 'and' and 'or'. Two prime examples:

>> val = 10

=> 10
>> ok = val < 3 or val > 5

=> true
>> ok

=> false

>> a = true

=> true
>> b = not a

SyntaxError: compile error
(irb):7: syntax error, unexpected kNOT

Use '||' and '!' respectively and you won't have a problem.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      05-09-2011
Brian Candler wrote in post #997379:
>
> Use '||' and '!' respectively and you won't have a problem.
>


lol.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Phillip Gawlowski
Guest
Posts: n/a
 
      05-09-2011
On Mon, May 9, 2011 at 10:11 PM, 7stud -- <> wrote:
> Brian Candler wrote in post #997379:
>>
>> Use '||' and '!' respectively and you won't have a problem.
>>

>
> lol.


Should've tried the code first:

irb(main):001:0> val = 10
=> 10
irb(main):002:0> ok = val < 3 || val > 5
=> true
irb(main):003:0> ok
=> true
irb(main):004:0> a = true
=> true
irb(main):005:0> b = !a
=> false

Using *only* "or" or "not" obviously doesn't lead to the expected
results, while using || and ! do.

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

 
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
How to let a loop run for a while before checking for break condition? Claudio Grondi Python 16 08-30-2006 05:40 AM
How to let a loop run for a while before checking for break condition? Sorin Schwimmer Python 1 08-28-2006 06:03 PM
Help Needed : Race condition while removing semaphore(sysvsem semaphore) techi_C C Programming 2 08-10-2006 12:27 PM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while condition Daniel VHDL 7 05-23-2005 02:29 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