FAQ: Control Flow in Ruby - And

This community-built FAQ covers the “And” exercise from the lesson “Control Flow in Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise And

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2**3 hadn’t been used before (unless I missed it) and should have been explained, so in the example =

boolean_3 = 23 == 8 && 32 == 9

2**3 == 8 can be explained as 2 * 2 * 2 ==8.

2 Likes

I tried out using a single & instead of && to see what happens, and it looks like they operate the same in logical expressions. Is that true or am I missing something? Is there a difference between & and && as boolean operators? Thank you!

If you play around with a few examples, you will see that & and | act differently than && and ||.

&& and || are logical operators, whereas & and | can be used for bitwise comparisons.

Consider a few examples:

# Comments show ouput

puts 3 && 4
# 4
puts 3 & 4
# 0

puts 3 || 4
# 3
puts 3 | 4
# 7

puts true && 4
# 4
puts true & 4
# true

puts true || 4
# true
puts true | 4
# true

puts 4 && true
# true
puts 4 & true
# true can't be coerced into Integer (TypeError)

puts 4 || true
# 4
puts 4 | true
# true can't be coerced into Integer (TypeError)

Have a look at:

As the stackoverflow thread shows, & can be used in contexts other than bitwise comparisons. For example, when used with arrays, & and | can be used for set intersection and union respectively.

x = [1, 3, 4, 5]
y = [1, 2 ,3, 8]

# &&, || (Logical)
puts x && y 
# [1, 2, 3, 8]
puts x || y
# [1, 3, 4, 5]

# &, |
puts x & y # Intersection
# [1, 3]
puts x | y # Union
# [1, 3, 4, 5, 2, 8]

Edit: When dealing with boolean values true and false, then

puts true && true # true
puts true & true # true

puts true || true  # true
puts true | true  # true

puts true && false # false
puts true & false # false

puts true || false # true
puts true | false # true

puts false && true # false
puts false & true # false

puts false || true # true
puts false | true # true
2 Likes