Arduino IDE: Conditional (if-else-if) Statements

Arduino IDE if-else-if Statement
Description
Learn the fundamentals of conditional statements in programming including if, if-else, and if-else-if statements. Understand how these statements can be used to make your program very powerful and be able to be used for a vast variety of purposes.

Introduction

Conditional statements check whether a programmer-specified Boolean condition is true or false. They make it possible to test a variable against a value/compare a variable with another variable and make the program act in one way if the condition is met, and another if it isn’t. They make the program very powerful and be able to be used for a vast variety of purposes.

This tutorial discusses the following conditional statements:

  • if statement
  • if-else statement
  • if-else-if statement.

The if statement

Given below is the structure of an if statement:

If (conditional expression)  {
Body of the if statement
}

The conditional expression can be anything that can result either in true or false. If the statement is true, the code in the body of the statement is executed. However, if the expression turn out as false, the code in the body is simply skipped.

Below is an example showing how to use the if statement:

int Student1_Marks = 92;
int Student2_Marks = 20;

void setup() {
 Serial.begin(9600);
 
 if (Student1_Marks > 33){
 Serial.println("The student1 marks is greater than 33. He passed the exam.");
 }
 if (Student1_Marks <= 33){ 
 Serial.println("The student1 marks is less than or eqaul to 33. He failed the exam."); } if (Student2_Marks > 33){
 Serial.println("The student2 marks is greater than 33. He passed the exam.");
 }
 if (Student2_Marks <= 33){
 Serial.println("The student2 marks is less than or eqaul to 33. He failed the exam.");
 }
}

void loop() {
 
}

Result on the Serial Monitor:

The student1 marks is greater than 33. He passed the exam.

The student2 marks is less than or eqaul to 33. He failed the exam.

In the code above, Student1 has marks greater than 33; hence, the first statement is true and is executed. For Student2, the second statement is true; hence, the second statement is executed.

evive Explore
Try interchanging numbers and then see what happens.

The if-else statement

When using an if statement, the code in its body runs only when the if statement evaluates to true. If it evaluates to false, program execution skips the code in the body of the if statement and goes to the statement the body of the if statement.

By adding an else statement, the code in the body of the else statement will run, but only when its corresponding if statement evaluates to false.

If (conditional expression)  {
Body of the if statement when the conditional expression is true
}
else {
Body of the else statement when the conditional expression is false
}

When the conditional expression evaluates to true:

  • Code in the body of the if statement runs.
  • Code in the body of the else statement does not run.

When the conditional expression evaluates to false:

  • Code in the body of the if statement does not run.
  • Code in the body of the else statement runs.

Below is an example showing  how to use the if-else statement:

int Student1_Marks = 92;
int Student2_Marks = 20;

void setup() {
 Serial.begin(9600);
 
 if (Student1_Marks > 33){
 Serial.println("The student1 marks is greater than 33. He passed the exam.");
 }
 else{
 Serial.println("The student1 marks is less than or eqaul to 33. He failed the exam.");
 }
 
 if (Student2_Marks > 33){
 Serial.println("The student2 marks is greater than 33. He passed the exam.");
 }
 else{
 Serial.println("The student2 marks is less than or eqaul to 33. He failed the exam.");
 }
}

void loop() {
 
}

Result on the Serial Monitor:

The student1 marks is greater than 33. He passed the exam.

The student2 marks is less than or eqaul to 33. He failed the exam.

The if-else-if statement

The if-else-if statement allows more than one conditional expression to be evaluated than the if-else statement.

Below is the basic structure:

if (conditional expression 1)  {
Body of the if statement when conditional expression 1 is true
}
else if (conditional expression 2)  {
Body of the else-if statement when conditional expression 1 is false and conditional expression 2 is true
}
else {
Body of the else statement when conditional expression 1 and 2 are both false
}

When conditional expression 1 evaluates to true:

  • Code in the body of the first if statement runs.
  • Codes in the body of the else-if statement and else statement doesn’t run.

When conditional expression 1 evaluates to false and conditional expression 2 evaluates to true:

  • Code in the body of the else-if statement runs.
  • Codes in the body of the if statement and else statement doesn’t run.

When both conditional expression 1 and 2 evaluate to false:

  • Code in the body of the else statement runs.
  • Codes in the body of the if statement and if-else statement don’t run.

Below is an example showing how to use the if-else-if statement:

int Marks = 95;
char Grade;

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

if (Marks <= 33){
 Grade = 'E';
 }
 else if (Marks <= 40){
 Grade = 'D';
 }
 else if (Marks <= 60){
 Grade = 'C';
 }
 else if (Marks <= 80){
 Grade = 'B';
 }
 else if (Marks <= 100){
 Grade = 'A';
 }

Serial.print("Your grade is: ");
 Serial.println(Grade);
}

void loop() {

}

Result on the Serial Monitor:

Your grade is: A

evive Explore
Try interchanging the marks and then see what happens.

Conclusion

In conclusion, conditional statements are an important part of programming and are used in many different situations. In this lesson, we discussed the if statement, if-else statement, and if-else-if statement. These statements make it possible to test a variable against a value or compare a variable with another variable, and make the program act in one way if the condition is met, and another if it isn’t. With these conditional statements, the program can be made very powerful and be able to be used for a vast variety of purposes.

Table of Contents