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

About This Tutorial
This tutorial discusses what are conditional statements, and their different types in Arduino IDE, such as the if statement, if-else statement, and if-else-if statement.
Tutorial Info
Programming Platform:
Difficulty:
Estimated Time (hrs):
2

Components Required

ImageComponentQuantityAvailable in Kit
evive1
USB A-B Cable1

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 which 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:

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 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 conditional expression is true
}
else {
Body of the else statement when 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:

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 expressions 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 don’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 don’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:

Result on the Serial Monitor:

Your grade is: A

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

 

This Helped
This didn't help
Share this Tutorial
Facebook
Twitter
Google+
Pinterest
Reddit
Print
Related Tutorials
Featured Projects

Leave a Reply

Close Menu
×

Cart