You are on page 1of 12

CHAPTER III

Arduino_Conditional Statements
ABE 13
What you are going to study in
this Chapter?
1. Familiarize themselves with different logical statements.
2. Construct the circuit using the different logical combinations.
3. Create the program or codes using the different logical statements.
4. Build a circuit showing the output characteristics of each logical statement.
What you are going to study in
this Chapter?
1. IF statements
2. Else statements
3. While statements
4. Combinations using the Logical Propositions (And, Or and Not)
5. For Statements .
Conditional Statements
• Conditional statements function similarly to tests in that they
determine whether or not a condition is true. If the condition
is met, the code contained inside the conditional expression is
run. The code is not performed if the condition is false. The if
statement is the most widely used conditional expression.
• This statements allows the users to control the flow of a
program based on certain conditions that you can define in
the code.
Conditional Statements
• “IF” statement
-Typically, if statements are put inside the loop() section, where
they are evaluated once for each iteration. This is the code for
an if statement.

if (condition) {
body;
}
-The code included in the body is only run if the condition is
met. The usual form of the conditional statement tests whether
or not the variable in question is less than, larger than, or equal
to a certain digit. No matter what condition is employed, it must
provide a true or false result.
Conditional Statements
• “IF” statement
Conditional Statements
• “IF” statement
list of all relational operators that can be used in conditional
statements:

x > y: x is greater than y


x < y: x is less than y
x >= y: x is greater than or equal to y
x <= y: x is less than or equal to y
x == y: x equals y
x != y: x is not equal to y
Conditional Statements
• “IF ELSE” statement
-if else statements first evaluate the condition. If the condition is
true, the code inside the body of the if block will be executed. If
the condition is false, the code in the else block will be
executed.
if (condition 1) {
// code executed if condition 1 is true
}
else if (condition 2) {
// execute code only if condition 1 is false and condition 2 is
true
}
Conditional Statements
• “IF ELSE” statement
Conditional Statements
• “IF ELSE” statement
-The program first checks condition 1 in the if statement. If it’s true,
the code in the body of the if statement is executed, then the program
exits the entire if else if block.

If condition 1 is false, the program checks condition 2 in the else if


block. If condition 2 is true, the code inside the body of the else if
section will be executed, then the program will exit the if else if block.

The if else if statement is similar to having two if statements, but


there’s a subtle difference. If else if only executes one block – the if
block or the else if block. The first block that has a true condition gets
executed. With two if statements, both would be executed if both of
the conditions were true.
Conditional Statements
• “NESTED IF ELSE” or “ IF ELSE IF” statement
-A nested if statements is like saying “if x is true, perform this action,
and then if y and z are true, perform this other action”.
-Nested if statements are frequently used when working with sensors.

int input = digitalRead(sensorPin);

if (input == HIGH) {
digitalWrite(ledPin, HIGH)
if(temp < 20) {
Serial.print("Low Temperature!");
}
if(temp > 20){
Serial.print("High Temperature!");
}
}

You might also like