Monday, May 20, 2013

Ruby Lesson 2 Section 2 / 3 of 4: Making Comparisons and Boolean Operators

Double sections! I know you're excited =)

Section 2 Topics

  • Equal / Not Equal 
  • Less Than / Greater Than 

This section is more about definitions as it is fairly straight forward. 

We are going to learn about comparators, also known as relational operators. They are as follows: 

  • Equal to "=="
  • Not equal to "!="
  • Less than "<"
  • Less than or equal to "<="
  • Greater than ">"
  • Greater than or equal to ">="

Most of these are self explainatory. The ones that are a bit different would be the first 2. The "==" comparator is written with two equal signs because we use the single equal sign to assign a variable. The not equal symbol, "!=", is also strange. "!" stands for not in this case, remember to be careful when placing !s because if you remember from previous lessons it is also used to replace a string when added to at the end of the ".capitalize" method.

Section 3 Topics

  • and "&&"
  • or "||"
  • not "!"

Remember that Booleans are values that are either true or false. Using a boolean operator will return a true or false answer. 

For those who are wondering the or sybol is not two lowercase "l"s it is actually the symbol on top of the "\" key (which is usually above the enter key"). I don't know why they double each symbol, my guess is that the single use of the symbol does something else. 

&& will only result in a "true" if both statements have are true. 

Examples of results: 
  1. true && true -> true
  2. true && false -> false
  3. false && false -> false 

|| will result in a "true" when either statement is true. 

Examples of results:
  1. true || true -> true
  2. true || false -> true
  3. false || false -> false

! will reverse the statement. What this means is if something is true putting a "!" in front of it will make it false. Think of it like putting a negative "-" sign in front of a number in a math equation. If you add one to a positive number it makes it negative, if you add it to a negative number it makes it a positive. 

* Note I tested it out and it seems that we can add as many "!" infront of a statement as we would like. So "!!true" will equal true. 

No comments:

Post a Comment