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 Operators and Control Structures Ruby Operators Ruby Comparison Operators: Greater and Less Than or Equal To

alexandermason
alexandermason
713 Points

&& || what do they mean? You haven't explained them at all ?

I want to know what they mean? Why aren't they explained? Great course though...

alexandermason : I just wanted to let you know 8 months in the future that the video was never edited to include that critical information. I rated his video 1 star and explained why, which is that AND (&&) and OR (||) operators are extremely important concepts to understand as they're one of the ways we compare things against each other. I was very unhappy that he left out all information about either....and I'm more unhappy that almost a year later nothing has changed.

5 Answers

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

they are comparison operators. && means AND and || means OR. They are, for example, used in the condition of an if statement.

Maximiliane Quel
Maximiliane Quel
Courses Plus Student 55,489 Points
if condition1 && condition2
    #do this
else if condition3 || condition4
    #do that
else
    #do yet another thing
end
Grzegorz Gancarczyk
Grzegorz Gancarczyk
10,064 Points

I think sooner or later you will have to understand how 'logic' in a math point of view works. But it's not as complex as it looks like.

In the tables I linked you:

  1. '~' is a negation, so '!' in a programming
  2. '^' means AND and it is represented by && in programming
  3. this down arrow which I don't know how to write using keyboard stands for OR and is represented by || in programming

It's really simple, take a look at the second table (the one with '^', which we can call 'AND tabel'): assume we have two variables, if both of them are true then the whole sentence would return a true. If we will get true and false the sentence can't be true, can it? Like, it would be half-true then, which makes no sense and still wont return true. It makes even more sense if we will take a look at the last row, which is false AND false. There is no 'true' in this sentence, so it can't return true.

EXAMPLE: sencence no 1: every cow is green; sentence no 2: 2 + 2 = 4. SOLUTION: sentence no 1 is not true, because we can have brown cows too so it will return FALSE. sentence no 2 is true obviously and it will return TRUE. So fianlly we have: FALSE + TRUE in this case returns FALSE.

EXAMPLE 2: sentence no 1: Sun is bigger than Earth. sentence no 2: there is a cow which is brown. SOLUTION: sentence no 1 is true obviously. sentence no 2 is also true, because there is AT LEAST 1 cow on the whole earth which is brown. So we have TRUE + TRUE which will result in the whole sentence returning TRUE.

With OR or 'down arrow' operator the situation changes. Now we need AT LEAST one sentence to be true.

EXAMPLE: 1) cows are blue 2) there is a blue car SOLUTION: the whole sentence will return true, because while cows are not blue so 1) is FALSE the second sentence will return TRUE (there is at least one blue car) so it will look like this: FALSE + TRUE = TRUE

In OR sentences we need at least a little bit of 'truth' to have a whole sentence returning true. But if we have: 1) cows are blue and 2) birds are blue then both of these sentences are FALSE so we have FALSE + FALSE. There is no truth here so the whole sentece will return FALSE.

And the last operator - negation (!): it's the easiest one: if we'd negate this sentence: 1) cows are blue, we will get: !1) cows are NOT blue. So negated TRUE becomes FALSE and negated FALSE becomes TRUE.

Grzegorz Gancarczyk
Grzegorz Gancarczyk
10,064 Points

They come from 'logic' part of math. Take a look at this: http://www.millersville.edu/~bikenaga/math-proof/truth-tables/truth-tables.html

&& and || are just these logical operators represented by another signs.

Also, sorry for my poor English.

alexandermason
alexandermason
713 Points

Im sorry but the table you have shown me just adds to the complication! When trying to learn something new, that does nothing for me! However the ''&& and || are just these logical operators represented by another signs'' would have sufficed just fine.

I am not learning to be a professor nor am i interested in being one.

But I do appreciate your answer.