0% found this document useful (0 votes)
98 views

Arduino - Decision Making Statements

This document discusses different types of decision making statements in Arduino programming including IF, IF-ELSE, IF-ELSE IF, and nested IF statements. IF statements execute code if a condition is true. IF-ELSE statements add an ELSE block that executes if the condition is false. IF-ELSE IF statements allow specifying multiple conditions in a chain. Nested IF statements can place IF statements within other IF statements. Relational operators like == and != are used to build conditional expressions that evaluate to true or false.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Arduino - Decision Making Statements

This document discusses different types of decision making statements in Arduino programming including IF, IF-ELSE, IF-ELSE IF, and nested IF statements. IF statements execute code if a condition is true. IF-ELSE statements add an ELSE block that executes if the condition is false. IF-ELSE IF statements allow specifying multiple conditions in a chain. Nested IF statements can place IF statements within other IF statements. Relational operators like == and != are used to build conditional expressions that evaluate to true or false.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

ARDUINO

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:

there isn’t any output since 'a' is not equal to 'b'.


However, if we were to modify the Boolean
expression so that the result would be true then
the code would be,
IF statements
Output

Here the Boolean expression is now “a == (b – a)” and


since ‘a’ is 10 and (b – a) is 10, the result would be true.
IF statements
Another Example:

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: what we refer to as "statements" can


be anything from a simple message like we
used to advance mathematical computations.
IF-ELSE IF statements
 IF-ELSEIF statements are a bit similar to IF-ELSE statements but instead of the ELSE statements
being executed by default if the Boolean expression is false, another condition must be satisfied
before the alternative action can be completed.
Example: "IF I am hungry then I will eat a burger, ELSE IF I am thirsty, I will drink some water.“
 The alternative statement "I will drink some water" is dependent upon the condition "If I am
thirsty".
 However, just like IF-ELSE statements, if the first condition is true then the ELSE IF part will be
ignored.
The syntax (the format used) for IF-ELSE IF statements:
IF-ELSE IF statements
Example
IF-ELSE IF statements
Output: If you have many ELSE IF statements, the syntax for such a case would be,
IF-ELSE IF statements
An ELSE statement can be used after and only after the ELSE IF statement(s).
The syntax would be:

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.

The syntax for this case is:


variable = (condition) ? expressionTrue : expressionFalse;
 The expressionTrue will be executed if the condition is true and the expressionFalse will be
executed if the condition is false.
END OF DISCUSSION

You might also like