Beginner Question: "and" "or" conditons

Hello,

Quick question about how "and " & “or” work in conditions.

They seem to work oppositely.

Examples:

"
var1 = 0
var2 = 0

while var1 != 3 and var2 != 3

puts ‘Num 1’
var1 = gets.chomp.to_i
puts ‘Num 2’
var2 = gets.chomp.to_i

end"

^ In this if I input either a x,3 or a 3,x I end the loop.

"
var1 = 0
var2 = 0

while var1 != 3 or var2 != 3

puts ‘Num 1’
var1 = gets.chomp.to_i
puts ‘Num 2’
var2 = gets.chomp.to_i

end"

^ In this if I must input 3,3 to end the loop.

So shouldn’t “and” demand a 3,3 and “or” either one a 3?

Thanks

Way of work is fine, what’s your worry? An advice: use parenthesis, and
try to use && and ||, they have higher presendece than “and” and “or”,
unless you want to nest this operators for some purpose.

Damián M. González wrote in post #1173516:

Way of work is fine, what’s your worry? An advice: use parenthesis, and
try to use && and ||, they have higher presendece than “and” and “or”,
unless you want to nest this operators for some purpose.

Thanks for your reply and helpful tips.

My question is why “and” in “while” conditions works as either and not
both and
“or” the opposite?

Ex.
"
var1 = 1
var2 = 1

if var1 == 1 and var2 == 1

puts true

else
puts false

end"

^ I tried this and it works correctly the “and” demands both and “or”
demands either.

Is it because “while” doesn’t accept multiple conditions?

Thanks again

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs