Lesson 5: Writing Conditional(if/else) Statements

Introduction:

Hello and Welcome to Electro Nerds Academy! In this lesson, we’ll explore how to write conditional statements (if, else & else if statements). These statements allows us to make decisions in our code based on a condition. We also use conditional statements in real life for example,

🔹If it rains, we take an umbrella.
🔹Else, we go outside without one.

Here, “raining” is a condition, and based on whether it’s true or false, we decide what action to take. meaning whether we take an umbrella or not. In programming, we do the same thing using if /else statements! here we also have a condition to check and based on whether it’s true or false, we decide what action to take. Therefore In any conditional statement, the condition plays the most important role, as it determines the next steps in decision-making. In programming, we define conditions using comparison operators.

Understanding Comparison Operators:

Comparison operators are used in Arduino to compare two values. Just like the result of an addition operator(+) is the sum of the two values, similarly The result of a comparison operator is always either true (1) or false (0). Below is a list showing all the comparison operators that are available to us in the Arduino Eco-System.

OperatorMeaningExampleResult
>Greater than5 > 6False
<Less than5 < 6True
>=Greater than or equal to9 >= 10False
<=Less than or equal to10 <= 10True
==Equal to2 == 3False
!=Not equal to2 != 3True

In the table above we can see that the first operator is greater than operator (>). And in the example we are checking whether “5” is greater than “6”. The result of this statement is false. As “5” is indeed not greater than 6. The next operator we have is the less than operator (<). And in the example we are checking whether “5” is less than “6”. The result of this statement is True. As “5” is indeed less than “6”. In similar fashion you can determine the result for the remaining operators.

💡 Note: That the equal to comparison operator uses two equal signs (==) rather than a single one. because a single equal sign is used as an assignment operator. meaning if we write, (A = 5) It means assign the value of "5" to the variable named "A". And if we write (A == 5) It means check whether the value of variable "A" is equal to "5".

Writing if/else Statements:

Now that we have learned how we write a condition using comparison operators. Lets examine the syntax of if-else statements which is shown below,

if (condition) 
{
// Code runs if the condition is true
}
else
{
// Code runs if the condition is false
}

We first have the ” if “ identifier followed by the “condition” we want to evaluate. If the condition returns True. then the code written between its curly brackets { } will be executed. And If the condition is false, the program moves to the “else” statement and executes the code inside its curly brackets { } instead.

Example-1:

Lets consider an example shown below here we have an integer variable “A” having the value “5”. then we have an if-else statement checking whether (A > 4). Since A is equal to 5. The condition returns True and Code A will get executed.

int A = 5;
if (A > 4) 
{
   // Code A
} 
else 
{
   // Code B
}

If the value of “A” is changed to “2”. Then the condition returns false as “2” is not greater than “4”. Therefore Code B gets executed.

int A = 2;
if (A > 4) 
{
   // Code A
} 
else 
{
   // Code B
}

Example-2: (Using Multiple If Statements)

We can include any number of “if” statements in our code, However only one “else” statement is permissible. So If none of the “if” conditions are met, the “else” statement executes.

In the code below, variable “A” is assigned a value of “4”. And there are two if statements, each checking a different condition. The first condition checks whether A is less than 4, and the second condition checks whether A is not equal to 5. Since A is equal to 4, the first condition evaluates to false because 4 is not less than 4. The program then moves to the second condition, which evaluates to true because 4 is not equal to 5. As a result, “Code B” inside the second if statement executes, and the else block is ignored.

int A = 4;
if (A < 4) 
{
   // Code A
}
if (A != 5)
{
   // Code B
}
else 
{
   // Code C
}

However, if the value of A is changed to 5, the first condition remains false since 5 is not less than 4. The program then moves to the second if condition, which also evaluates to false because A is equal to 5. Since both if conditions are false, the else block executes, and “Code C” runs.

int A = 5;
if (A < 4) 
{
   // Code A
}
if (A != 5)
{
   // Code B
}
else 
{
   // Code C
}

We can also omit the else block completely and solely employ ” if “ statements as shown below, Now if “A” is assigned a value of “5” nothing happens since both if conditions fail.

int A = 5;
if (A < 4) 
{
   // Code A
}
if (A != 5)
{
   // Code B
}

Example-3:

In this example we will check in which range our variable “A” falls into. If its value is between 0 and 10, code A should execute; if it’s between 10 and 25, code B should run; and if it’s between 25 and 50, code C should be executed.

The code for the example is shown below, we have three separate if statements. The first if statement checks whether A is less than 10—if true, code A executes. The second if statement checks whether A is less than 25—if true, code B runs. Finally, the third if statement checks whether A is less than 50—if true, code C executes.

int A ;
if (A < 10) 
{
   // Code A
}
if (A < 25) 
{
   // Code B
}
if (A < 50) 
{
   // Code C
}

At first glance, this may seem correct, but let’s examine what happens when we assign different values to variable “A”,

  • If A is set to 45, the first two conditions are false, but the third condition is true, so only code C executes—this works as expected.
  • If A is set to 15, the first condition is false, but the second condition is true, so code B executes. However, when the Arduino checks the third condition, it is also true since 15 is less than 50. This means both code B and code C execute, which is not what we intended.
  • The issue becomes even clearer when A is set to 5—all three conditions are true because 5 is less than 10, 25, and 50, so the code block of all three if statements run.

To solve this issue, we introduce the “else-if” statements.

else-if Conditional Statements:

These statements allow conditions to be checked sequentially in a structured manner. Their syntax looks something like this.

if (condition 1) 
{
// Code runs if the condition 1 is true
}
else if (condition 2)
{
// Code runs if condition 1 is false and condition 2 is true
}
else if (condition 3)
{
// Code runs if condition 2 is false and condition 3 is true
}

It is always started with an ” if “ statement followed by as many “ else-if ” statements we want. Here, the program first checks the ” if “ condition—if true, it executes the corresponding block of code and skips all other conditions. If false, it moves to the first “else-if” condition, the program again checks the condition—if true, it executes the corresponding block of code and skips all other conditions. If this condition is also false, the program continues checking subsequent  “else-if” conditions. This process continues until a condition evaluates as true or all conditions are checked. This structure ensures that only one block of code executes, preventing multiple statements from running simultaneously.

Now, let’s re write example-3 using “else-if” statements and see how it will resolve our initial issue. We will replace the second and third “ if ” statements with “else-if” statements as shown.

int A ;
if (A < 10) 
{
   // Code A
}
else if (A < 25) 
{
   // Code B
}
else if (A < 50) 
{
   // Code C
}

Now let’s test the code by assigning different values to variable “A”,

  • If A is set to 5, the first condition is evaluated as True, And code A is executed and the remaining conditions are ignored, ensuring only one block of code runs at a time which is exactly what we wanted.
  • If A is set to 15, the first condition is now false, So the program moves towards the first ” else if ” statement. This condition is true, so code B executes and the rest of the conditions are ignored.
  • If A is set to 45, the first two conditions are now false, So the program moves towards the second ” else if ” statement. This condition is true, so code C executes.

The else if structure ensures that as soon as a condition evaluates to true, the remaining conditions are skipped, preventing multiple blocks of code from executing.

Combining Conditions with Logical Operators

So the else if statements are great when we have to check our conditions in a structured manner one after the other. But what if we want to check two, three or even more conditions in a single if statement, for example if we want to check whether variable A is less than five(A < 5) and greater than one(A > 1) using a single if statement. In that case, we can combine conditions using logical operators: AND, OR, and NOT.

🔹AND ( && ) Operator :

So let’s start with the AND operator, represented by two ampersands (&&). When conditions are combined using the AND operator it ensures that all conditions must be true for the overall expression to be true. For example, lets combine the two conditions discussed above that is (A < 5) and (A > 1) using an AND operator, And place this whole expression inside an ” if “ statement as shown below.

if ((A < 5) && (A > 1))
{
   // Code B
}

Now the if block will be executed only when the whole expression will be true which will be true only if both conditions are true. If either condition is false, the entire expression evaluates to false. So if we assign variable “A” a value of “4”. Then the if statement is evaluated as True because 4 is both greater than 1 and less than 5. However, if the value of “A” is changed to “6”, the if statement is evaluated as false because although 6 is greater than 1 but it is not less than 5. And since the AND operator requires both conditions to be true, the overall expression is false.  

🔹OR ( || ) Operator :

Next we have the OR operator, which is represented by two vertical lines. The “OR” operator requires at least one condition to be true for the whole expression to be evaluated as true. If both conditions are false, only then the expression evaluates to false. So, let’s replace the AND operator with an OR operator in the previous example as shown below,

if ((A < 5) || (A > 1))
{
   // Code B
}

Now if we assign “A” a value of “6”, the Code B inside the if block will now be executed. This happens because even though the first condition is false, the second condition is true, and since the OR operator only requires one condition to be true, the overall expression is true and the Code B is executed. 

🔹NOT ( ! ) Operator :

The NOT operator, represented by an exclamation mark, negates the output of a condition. If a condition is true, it makes it false, and if a condition is false, it makes it true. For example, if we check whether 5 is less than 6, the condition evaluates to true, meaning the code inside the block will execute.

if (5 < 6) 
{
   // Code A
}

However, if we place a NOT operator in front of the condition, it will reverse the result, making the condition false, and the code inside the block will not be executed.

if (!(5 < 6)) 
{
   // Code A
}

Conclusion:

That’s all for this lesson! In this lesson, we have learned:

✅ What conditional statements are and how they help in decision-making in the code.

✅ We learned about different comparison operators (<, >, <=, >=, ==, !=) and their role in formulating conditions.

✅ How to write if, else, and else-if statements to control the flow of our program.

✅ The importance of else-if in preventing multiple conditions from executing at the same time.

✅ How logical operators (&&, ||, !) allow us to combine multiple conditions in a single statement.

In the next lesson, we’ll explore how to interface a button with Arduino and detect button presses! If you want to refresh your knowledge about variables, be sure to check out the previous lesson.

For more tutorials, visit our website and YouTube channel. And don’t forget to share it with your friends—we’ll see you in the next lesson! 🚀

Leave a Comment

Your email address will not be published. Required fields are marked *