Conditions

Deciding the Truth

SparkiDuino
Easy

Lessons You Should Know

Conditions are statements that turn into true or false (kind of like booleans). They usually look like questions or equations, but sometimes they may just be a single variable. Everything in a computer turns into true or false eventually and the way a robot figures out if a question is true or false is by using conditions.Here’s what we’ll talk about on this page:  We’ll talk about the conditions that look like equations below. Before we do that let’s talk about the conditions that look like a single variable. Sometimes you’ll see a single variable inside of parentheses. These variables will always need to be boolean variables. Because boolean variables are equal to either true or false you don’t need to use an equation to ask a question! The variables itself is the true or false.You build a condition by using symbols called logic operators. Here are the basic logic operator types:== equalsTests to see if the statement on the left is equal to the statement on the right. If the are equal, the condition is true. If they are not equal, the condition is false.(2 == 3) is false (3 == 2 ) is false (3 == 3) is trueImportant! Make sure to keep in mind the difference between a single equals sign = and a double equals sign ==. A single equals sign is used to assign (or create) a value for a variable but a double equals signs asks a question. You can think of it this way if you have a hard time, remember that-
  • Just one equals sign is going to be really bossy. It tells variables and values what to do.
  • Two equals signs won’t be bossy and they will ask each other what they think.
 != does not equalTests to see if the statement on the left is not equal to the statement on the right. If they are not equal, the condition is true. If they are equal, the condition is false. This is the exact opposite of the double equals signs.(2 != 3) is true (3 != 2 ) is true (3 != 3) is false < less thanTests to see if the statement on the left is less than the statement on the right. If the statement on the left is less than the one on the right, the condition is true. If the statement on the left is greater than or equal to the one on the right, the condition is false.(2 < 3) is true (3 < 2 ) is false (3 < 3) is falsealligatorless01Think of this logic operator like an alligator that always wants to eat the bigger meal. As long as the mouth of the alligator is about to eat the larger value, the alligator is happy and the outcome is true. Otherwise you’ve got an angry alligator who knows there is a larger meal right behind it, so the answer is false! <= less than or equal toTests to see if the statement on the left is less than or equal to the statement on the right. If the statement on the left is less than or equal to the one on the right, the condition is true. If the statement on the left is greater than the one on the right, the condition is false.(2 <= 3) is true (3 <= 2 ) is false (3 <= 3) is true > greater thanTests to see if the statement on the left is greater than the statement on the right. If the statement on the greater is less than the one on the right, the condition is true. If the statement on the left is less than or equal to the one on the right, the condition is false.(2 > 3) is false (3 > 2 ) is true (3 > 3) is falsealligatorGreater01This logic operator is also like an alligator that always wants to eat the bigger meal. As long as the mouth of the alligator is about to eat the larger value, the alligator is happy and the outcome is true. Otherwise you’ve got an angry alligator who knows there is a larger meal right behind it, so the answer is false! >= greater than or equal toTests to see if the statement on the left is less than or equal to the statement on the right. If the statement on the left is less than or equal to the one on the right, the condition is true. If the statement on the left is greater than the one on the right, the condition is false.(2 >= 3) is true (3 >= 2 ) is false (3 >= 3) is true 

Using Conditions Together

The above are all the basic condition types. You can also combine conditions together using these two operators:&& andTests to see if BOTH the condition on the left is true, AND the condition on the right is true. If either condition is false, the outcome is false.((1==2) && (1==2)) is false ((1==2) && (2==2)) is false ((2==2) && (1==2)) is false ((2==2) && (2==2)) is trueYou can also use as many ANDs as you want, just as long as they are inside a single set of parentheses:((1==2) && (1==2) && (3==4) && (4==3))The condition above is a valid condition, but the answer is still false|| orTests to see if EITHER the condition on the left is true, OR the condition on the right is true. As long as one of them is true, it is still true. Both of the conditions on either side of the OR symble must be false for the whole condition to be false.((1==2) || (1==2)) is false. ((1==2) || (2==2)) is true. ((2==2) || (1==2)) is true. ((2==2) || (2==2)) is true.You can also use as many ORs as you want, just as long as they are inside a single set of parentheses:((1==2) || (1==2) || (3==4) || (4==3))The condition above is a valid condition, but the answer is still false

Recap of What We’ve Covered So Far

orNow that we’ve gone over conditions, let’s get back to if statements.

Back to If Statements