Program Control Structures

Control Structure

if is a statement that checks the condition and executes the following statements if the condition is TRUE. There are multiple ways how to write down the if statement:

//1st example
if (condition) statement;     
 
//2nd example
if (condition)
statement;                    
 
//3rd example
if (condition) { statement; } 
 
//4th example
if (condition)
{
  statement;
}                             

When both TRUE and FALSE cases of the condition should be viewed, the else part should is added to the if statement in the following ways:

if (condition) {
  statement1;    //Executes when the condition is true
}
else {
  statement2;    //Executes when the condition is false
}

If more conditions should be viewed, the else if part is added to the if statement:

if (condition1) {
  statement1;    //Executes when the condition1 is true
}
else if (condition2) {
  statement2;    //Executes when the condition2 is true
}
else {
  statement3;      //Executes in all of the rest cases
}

The example when the x variable is compared and in the cases when it is higher than 10, the digitalWrite() method executes.

if (x>10) 
{
  //Statement is executed if the x > 10 expression is true
  digitalWrite(LEDpin, HIGH)  
}
Logical Operators

Logical operators are widely used together with the condition operator if that is described below.

Comparison Operators

There are multiple comparison operators used for comparing variables and values. All of these operators compare the value of the variable on the left to the value of the variable on the right. Comparison operators are the following:

  • == (equal to) – if they are equal, the result is TRUE, otherwise FALSE;
  • != (not equal to) – if they are not equal, the result is TRUE, otherwise FALSE;
  • < (less than) – if the value of the variable on the left is less than the value of the variable on the right, the result is TRUE, otherwise FALSE;
  • < = (less than or equal to) – if the value of the variable on the left is less than or equal to the value of the variable on the right, the result is TRUE, otherwise FALSE;
  • > (greater than) – if the value of the variable on the left is greater than the value of the variable on the right, the result is TRUE, otherwise FALSE;
  • > = (greater than or equal to) – if the value of the variable on the left is greater than or equal to the value of the variable on the right, the result is TRUE, otherwise FALSE.

Examples:

if (x==y){ //Equal
  //Statement
}
 
if (x!=y){ //Not equal
  //Statement
}
 
if (x<y){ //Less than
  //Statement
}
 
if (x<=y){ //Less than or equal
  //statement
}
 
if (x>y){ //Greater than
  //Statement
}
 
if (x>=y){ //Greater than or equal
  //Statement
}

Boolean Operators

Three Boolean logical operators in the Arduino environment are the following:

  • ! (logical NOT) – reverses the logical state of the operand. If a condition is TRUE the logical NOT operator will turn it to FALSE and the other way around;
  • && (logical AND) – the result is TRUE when both operands on the left and on the right of the operator are TRUE. If even one of them is FALSE the result is FALSE;
  • || (logical OR) – the result is TRUE when at least one of the operands on the left and on the right of the operator is TRUE. If both of them are FALSE the result is FALSE.

Examples:

//Logical NOT
if (!a) { //The statement inside if will execute when the a is FALSE
  b = !a; //The reverse logical value of a is assigned to the variable b
}
 
//Logical AND
//The statement inside if will execute when the 
//Values both of the a and b are TRUE
if (a && b){  
  //Statement
}
 
//Logical OR
//The statement inside if will execute when at least one of the 
//a and b values is TRUE
if (a || b){  
  //Statement
}
Switch Case Statement

Switch statement similar like if statement controls the flow of program. The code inside switch is executed in various conditions. A switch statement compares the values of a variable to the specified values in the case statements. Allowed data types of the variable are int and char. The break keyword exits the switch statement.

Examples:

switch (x) { 
   case 0:  //Executes when the value of x is 0
   // statements
   break;   //Goes out of the switch statement
 
   case 1:  //Executes when the value of x is 1
   // statements
   break;   //Goes out of the switch statement
 
   default:  //Executes when none of the cases above is true
   // statements
   break;   //Goes out of the switch statement
}

Check Yourself

1. Which code part is the correct one?

  • if(value == 1) digitalWrite(13, HIGH)
  • if (value == 1); digitalWrite(13, HIGH)
  • if (value == 1) DigitalRead(13,1)

2. What is the output of the next code part?

int x = 0;
 
    switch(x)
    {
 
      case 1: cout << "One";
 
      case 0: cout << "Two";
 
      case 2: cout << "Hello, world!";
 
    }
 

3. In which cases switch structure should be used?

en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/arduino_and_arduino_101_intel_curie/programming_fundamentals_rtu/program_control_structures.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0