10) If Statements

This is THE MOST IMPORTANT piece of programming!
an "if statement" looks like:
if (this==that)
{
  //do these actions
}

The == is one of many "comparison operators". The comparison operators are as follows:
  1. == checks if the two values are equal to each other
  2. > checks if the left value is larger than the right value
  3. < checks if the right value is larger than the left value
  4. >= checks if the value on the left is larger than or equal to the value on the right
  5. <= checks if the value on the right is larger than or equal to the value on the left
  6. != checks if the two values do not equal each other
(NOTE: the == are needed in if statments as apposed to one =... The one = sets the left value to the right value instead of the check command)

If you wanted to write "x is now 7" when the variable x is 7 than the if statement will look something like:
int x=0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  x++;
  if (x==7)
  {
    Serial.println("x is now 7");
  }
}

Notice that there are now "{" and "}" inside the void loop function. These "{" and "}" are used to clarify areas to which you are tying to refer to. And please indent like the example shown above! Whenever you introduce a "{", the next line needs to be indented. And the "}" line needs to decrease it's indentation. This rule of indentation is not required BUT HIGHLY RECOMMENDED. This stratagy of indentation is easier to look rather than no indentations. And remember to close off your "{" with a "}", if you forget, you will get an error message that says something like improper count of "}" (which error messages appear on the bottom of the Arduino's compiler environment in red if you haven't seen one already).

There are also logical operators for if statements, which will allow you to check multiple values in different ways. When each individual check is finished, it is marked as either a true or a false; true meaning that the check is good, and false meaning that the check is bad. With logic operators, such as the "and operator" (&&), you can see if the comparison operation on the left and right are both true! If they are, the operation follows through; if not, the code skips it.

This might sound confusing right now but if we had a count of cows and chickens as variables and we wanted to see if our number of cows is 17 AND our number of chickens are 349, we would do this:
if (cows==17 && chickens==249)
{
  Serial.println("All are accounted for");
}
This if statment will only write "All are accounted for" if we have BOTH variables equaling those amounts.
We can also see if we have at least 10 of each animal by using:
if (cows>=10 && chickens>=10)
{
  Serial.println("We have more than 10 of each.");
}

And if we want to say something if we don't have enough, than we can use an else right after the closing "}" of the if statement like so:
if (cows>=10 && chickens>=10)
{
  Serial.println("We have more than 10 of each.");
}
else
{
  Serial.println("We are short!");
}
If you want to check if you have more that 100 of any animal you can use the or to check if either the cows OR chickens have over 100 like:
if (cows>100 || chickens>100)
{
  Serial.println("We have reached over 100!!");
}
else
{
  Serial.println("Not yet..");
}
The operator logic is like this:
  1. AND &&
    1. true && true -> true
    2. true && false -> false
    3. false && true -> false
    4. false && false -> false
  2. OR ||
    1. true || true -> true
    2. true || false -> true
    3. false || true -> true
    4. false || false -> false
The OR symbol is two "pipe characters" which is "Shift" + "\"(back-slash, located below the backspace key).

Along with the "else" statement, you can also use "else if" which only is checked if the statements above it are skipped.
if (that==1)
{
  //action 1
}
else if (that==2)
{
  //action 2
}
else if (that==3)
{
  //action 3
}

You can also stick an else on the end of it if you need to have one.
Comments