0% Complete
0/25 Steps

Conditional Statements: If-else-if

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
}

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:

The 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
}

If else statement

When the conditional expression evaluates to true:

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

When the conditional expression evaluates to false:

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

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

The 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
}

if else if statement

When conditional expression 1 evaluates to true:

  • The code in the body of the first if statement runs.
  • Code 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:

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

When both conditional expression 1 and 2 evaluate to false:

  • The code in the body of the else statement runs.
  • Code 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:

The result on the Serial Monitor:

Your grade is: A

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

Conclusion

You have now understood how conditional statements work in Arduino IDE. In the next topic, we will use this to make LED glow whenever we press the switch.

Log in with your credentials

or    

Forgot your details?

Create Account