Arduino - Decision Making Statements
Arduino - Decision Making Statements
PROGRAMMING
DECISION MAKING STATEMENTS
Mathematical Operations Table
Mathematical Operations Table
We can use the Mathematical Operations Table for this example:
Mathematical Operations Table
Output:
Relational Operators
The result of expression that uses relational operators is either true or false.
Let 'a' be equal to 10 and 'b' equal to 20.
IF statements
This is the most commonly used variation of decision making statements and it depends upon whether a
condition is either true or false, before the task can be completed.
Example: If I am hungry then I will eat a burger.
the condition here is “if I am hungry” and the task is “I will eat a burger”. Only if the condition is true then
can the task be completed, After all, you shouldn’t be eating if you aren’t hungry.
In C++, the condition is usually a Boolean expression, or in other words, an expression which can be either true
or false and the task(s) is referred to as statement(s).
As you can see, the Boolean expression is
contained with the parenthesis and the
statement(s) are contained within the curly
brackets.
NOTE: when you started with programming you were to figure out which Boolean expression is considered as
true and which is considered as false. For now, just keep in mind that zero is considered as false and any value
greater than zero is true.
IF statements
Let’s take a look at this piece of code in the Arduino IDE:
int a = 10;
int b = 20;
void setup() { Serial.begin(9600); // starting communication between arduino board and computer }
void loop() {
if(a < b){ //checking to see if a is less than b
Serial.println("a is less than b");
}
if(a > b){ //checking to see if a greater than b
Serial.println("a greater than b");
}
if(a <= b){ //checking to see if a is less than or equal to b
Serial.println("a is less than or equal to b");
}
if(a >= b){ //checking to see if a is greater than or equal to b
Serial.println("a is greater than or equal to b");
}
if(a != b){ //checking to see if a is not equal to b
Serial.println("a is not equal to b");
}
Serial.println("_______________________________________________________");
delay(1000);
}
IF-ELSE statements
This is the second most common variation of decision-making statements used in programs.
Let's use our first example for reference:
"IF I am hungry then I will eat a burger or ELSE I will drink some tea.”
The "I will drink some tea" part of the statement is used as an alternative action that you will
do if you aren't hungry. Therefore, the alternative action can only be done if the condition,
which is "if I am hungry", is false.
Note: The use of an ELSE statement is optional.
IF-ELSE statements
The syntax (the format used) for IF-ELSE statements is:
If the Boolean expression is true then the ELSE statements would be ignored completely.
IF-ELSE statements
Example
IF-ELSE statements
The message printed on the screen is the Output
ELSE statement since the Boolean expression
is false.
You are free to experiment with the Boolean
expression and the different statements to
get different outputs.
Note: you can use as many ELSE IF statements you like in the above case but the ELSE statement
must be last or else you would end up with an error.
Nested IF Statements
An IF statement within an IF statement then this is totally possible and is used in many
applications.
The syntax for this case is:
Note: A common mistake that beginners make is that they tend to forget the curly brackets that
would enclose the second IF statement and it is usually the most common source of frustration.
Nested IF Statements
• The last variation of the IF statement we will be looking at is a short hand version of the IF-ELSE
statement and is referred to as the Ternary operator.