In Arduino programming, control statements are used when the execution of the code is controlled by some conditional statements. These control statements can be implemented by different statements like if statements, if-else statements, and switch-case statements. These statements in real life are very useful like we can control the operation of street lights; when it’s day time the lights should be switched off or else switched on. Similarly, if the temperature of a motor rises from its ambient temperature, the motor should be powered off for its protection.

In this write-up, these control statements are explained with the help of demonstration of flow charts as well as with simple examples.

What are the different types of control statements in Arduino

There are different types of control statements which are explained in detail one by one:

if statement: The “if statement” is the simplest form of the control statement in which the statements are written in the body of “if statement”, only if the condition is true then statements in the body will be executed else the compiler executes the next statements. The “if statement” is used when the operation is executed on the fulfillment of the single condition. The flowchart of using the “if statement” is:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/1-32.jpg" data-lazy- height="521" src="data:image/svg xml,” width=”341″>

In the flowchart, it is clear that if the condition is true then it will execute the statements in the body and if the condition is false, it will simply skip the statements and go to the next statements and execute them. Consider an example, if we want to print the “Welcome to LinuxHint” when the variable “a” has value 4 using the if statement, the code will be:

int a=4;

void setup(){

Serial.begin(9600);

if (a==4){

Serial.println(“Welcome to LinuxHint”);}

}

void loop(){

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/2-30.jpg" data-lazy- height="222" src="data:image/svg xml,” width=”704″>

The output will be:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/3-29.jpg" data-lazy- height="93" src="data:image/svg xml,” width=”760″>

If we change the value of variable a from 4 to 3, the output will be empty.:

Explanation: In the first code, we declare a variable “a” with value 4 and apply a if condition that if a==4, print “Welcome to LinuxHint”. In the first code, the condition is true so it printed the output on the serial output monitor.

if-else statement: The “if-else statements” is another type of control statement and the advanced form of “if statements”, works as “either-or” like if one scenario is false it will execute something else. Mostly, it is used in controlling the operation by using a check on it. For example, if the temperature of a room is below 30 degrees, turn on the green LED which means the temperature is normal, or turn on the red LED which means the temperature is above 30 degrees. The working of the “if-else statement” is demonstrated through the flowchart:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/4-26.jpg" data-lazy- height="541" src="data:image/svg xml,” width=”551″>

The working of the if-else statements is similar to the if statement, the only difference between both of them is that in “if statement” when the condition becomes false the body of the if statement is skipped whereas in an if-else statement, if the if statement is false, it will execute the statements of “else”. And if the condition of if statement is true, it will skip the else statement. Consider the following code of using if-else statement:

int a=3;

void setup(){

Serial.begin(9600);

if (a==4)

Serial.println(“The if statement executed”);

else

Serial.println(“The else statement executed”);

}

void loop(){

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/5-25.jpg" data-lazy- height="275" src="data:image/svg xml,” width=”784″>

The output will be:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/6-23.jpg" data-lazy- height="105" src="data:image/svg xml,” width=”859″>

Now, changed the value of variable “a” from 3 to 4:

int a=4;

void setup(){

Serial.begin(9600);

if (a==4)

Serial.println(“The if statement executed”);

else

Serial.println(“The else statement executed”);

}

void loop(){

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/7-20.jpg" data-lazy- height="280" src="data:image/svg xml,” width=”779″>

The output will be:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/8-18.jpg" data-lazy- height="97" src="data:image/svg xml,” width=”769″>

Explanation: In the above codes we simply write two print statements; one in the body of the if statement and the second in the body of the else statement. In the first code, the condition of if statement is false, so else part was executed and in the second code, the if statement was true, so statements written in the if the statement were executed instead of else statement and the output was printed on the serial output monitor at a baud rate of 9600.

Switch-case statements: In control statements of Arduino, one is the switch-case statements by which we can control the flow of the program. In switch-case statements, different cases are declared, if any of them becomes true, its body is executed, the compiler breaks and goes out of the switch-case body.

If no case is true then the default body will be executed. For example, we have two states of LED either on or off, so we will make two cases of “on” and “off”. If the LEDs are on, case 1 will execute and display the LEDs are on and if they are in an off state, case 2 will execute and display the LEDs are off. If both cases are not true, like the LEDs are not connected, it will display the default portion in which an “invalid state” will be displayed. The flowchart of the switch-case statements is:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/9-16.jpg" data-lazy- height="670" src="data:image/svg xml,” width=”560″>

The above flowchart clarifies the working of the switch-case statements. Now, consider the following code:

int a=1;

void setup(){

Serial.begin(9600);

switch (a) {

   case 1:

   Serial.println(“Case 1 executed”);

   break;

   case 2:

   Serial.println(“Case 2 executed”);

   break;

   default:

   Serial.println(“Default is executed”);

    }

}

void loop(){

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/10-15.jpg" data-lazy- height="349" src="data:image/svg xml,” width=”767″>

The output is:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/11-12.jpg" data-lazy- height="63" src="data:image/svg xml,” width=”835″>

Changed the value of variable a from 1 to 2:

int a=2;

void setup(){

Serial.begin(9600);

switch (a) {

   case 1:

   Serial.println(“Case 1 executed”);

   break;

   case 2:

   Serial.println(“Case 2 executed”);

   break;

   default:

   Serial.println(“Default is executed”);

}

}

void loop(){

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/12-12.jpg" data-lazy- height="355" src="data:image/svg xml,” width=”733″>

The output is:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/13-12.jpg" data-lazy- height="60" src="data:image/svg xml,” width=”487″>

Now, assign value to variable “a” other than 1 and 2:

int a=10;

void setup(){

Serial.begin(9600);

switch (a) {

   case 1:

   Serial.println(“Case 1 executed”);

   break;

   case 2:

   Serial.println(“Case 2 executed”);

   break;

   default:

   Serial.println(“Default is executed”);

}

}

void loop(){

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/14-9.jpg" data-lazy- height="351" src="data:image/svg xml,” width=”710″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/15-10.jpg" data-lazy- height="87" src="data:image/svg xml,” width=”641″>

The output will be:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/16-7.jpg" data-lazy- height="65" src="data:image/svg xml,” width=”654″>

Explanation: In the above codes, we have declared the variable “a” and then defined two cases depending on the value of a. If the variable “a” has value “1” case 1 will be executed as in code 1 and print “Case 1 executed”, if the variable “a” has value “2”, case 2 will be executed as in code 2 and print “Case 2 executed” and if any value other than 1 and 2 is stored in “a” then the cases will be skipped and default will be executed by printing “Default is executed”.

Conditional operator: Another type of control statement is “conditional operators” which uses the ternary sign “?” and decides on the basis of the condition which part should be executed. For example, if the motor is in a running state, it will turn on the green LED and if the motor is in a stop state, it will turn on the yellow LED. the flowchart of the working of the conditional statement will be:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/17-6.jpg" data-lazy- height="401" src="data:image/svg xml,” width=”631″>

If the condition is true for value 1, it will skip the value 2 and if the condition is false for value 1 it will skip value1 and execute the value2. For the better understanding of the conditional statements usage, type the code in Arduino IDE:

void setup(){

Serial.begin(9600);

int a=100, b=60, max;

max=(a>b)? a : b;

Serial.print(“The maximum number is: ”);

Serial.print(result);

}

void loop(){

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/18-6.jpg" data-lazy- height="254" src="data:image/svg xml,” width=”749″>

The output is:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/19-5.jpg" data-lazy- height="88" src="data:image/svg xml,” width=”682″>

Explanation: The above code is used to display the maximum number. Two numbers are stored in newly declared variables, a and b. By using the conditional operator, we are comparing the value of “a” with the value of “b”. If the value of “a” is greater than “b”, it will store the value of “a” in the variable “max” and display it on the serial output monitor. Else it will print the value of “b” using the serial communication at a baud rate of 9600.

Conclusion

The control statements are used to execute the code of Arduino in an organized manner. It will control the execution of the statements depending on certain conditions. In this write-up, the control statements in Arduino are explained with the help of examples. The working of the control statements is demonstrated with the help of flowcharts.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/hammad–150×150.jpg61f7a34b49bb2.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.