````

Arduino - If…else if …else statement

The if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
When using if...else if…else statements, keep in mind −
  • An if can have zero or one else statement and it must come after any else if's.
  • An if can have zero to many else if statements and they must come before the else.
  • Once an else if succeeds, none of the remaining else if or else statements will be tested.

if … else if …else Statements Syntax

if (expression_1) {
   Block of statements;
}

else if(expression_2) {
   Block of statements;
}
.
.
.

else {
   Block of statements;
}

if … else if … else Statement Execution Sequence

If Else If Else Statement

Example

/* Global variable definition */
int A = 5 ;
int B = 9 ;
int c = 15;

Void setup () {

}

Void loop () 
{
   /* check the boolean condition */
   if (A > B)
 /* if condition is true then execute the following statement*/
 {
      A++;
   }
   /* check the boolean condition */
   else if ((A == B )||( B < c) ) 
/* if condition is true then 
      execute the following statement*/ 
 {
      C = B* A;
   }else
      c++;
}

Arduino - If …else statement

An if statement can be followed by an optional else statement, which executes when the expression is false.

if … else Statement Syntax

if (expression) {
   Block of statements;
}
else {
   Block of statements;
}

if…else Statement – Execution Sequence

If Else Statement

Example

/* Global variable definition */
int A = 5 ;
int B = 9 ;

Void setup () {

}

Void loop () {
   /* check the boolean condition */
   if (A > B) 
 /* if condition is true then execute the following statement*/  
{
      A++;
   }else {
      B -= A;
   }
}

Arduino - Control Statements

Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program. It should be along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages −
Decision Making

 
Powered by Blogger