Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Loops Build a Simple Contact List Part 2: Adding Contacts

Alex Lowe
Alex Lowe
15,147 Points

Using OR with while statements

Is it possible to use OR with while statements? I tried to do it like this :

While answer.downcase != "no" || answer.downcase != "n"

It didn't work. IS there some other way, or am I doing something wrong?

2 Answers

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

One way to do this would be to use parentheses:

while (answer.downcase != "no" || answer.downcase != "n") do
  puts "I'm still going!"
end

The expression answer.downcase != "no" || answer.downcase != "n" is always true. If answer.downcase is "no", then answer.downcase != "n" is true, so the entire expression is true, and the while loop will continue. If answer.downcase is "n", then answer.downcase != "no" is true, so the entire expression is true, and the while loop will continue. If answer.downcase is not "n" and not "no", then both answer.downcase != "no" and answer.downcase != "n" are true, so the while loop will continue. For the expression to be false, answer.downcase == "no" and answer.downcase == "n" must both true at the same time. Since "no" is not equal to "n", the expression will always be true.

You could try while not (answer.downcase == "no" || answer.downcase == "n").