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:
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:
void setup(){
Serial.begin(9600);
if (a==4){
Serial.println(“Welcome to LinuxHint”);}
}
void loop(){
}
The output will be:
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:
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:
void setup(){
Serial.begin(9600);
if (a==4)
Serial.println("The if statement executed");
else
Serial.println("The else statement executed");
}
void loop(){
}
The output will be:
Now, changed the value of variable “a” from 3 to 4:
void setup(){
Serial.begin(9600);
if (a==4)
Serial.println("The if statement executed");
else
Serial.println("The else statement executed");
}
void loop(){
}
The output will be:
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:
The above flowchart clarifies the working of the switch-case statements. Now, consider the following code:
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(){
}
The output is:
Changed the value of variable a from 1 to 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(){
}
The output is:
Now, assign value to variable “a” other than 1 and 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(){
}
The output will be:
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:
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:
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(){
}
The output is:
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.