This is an old revision of the document!


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
}
if (x>10) 
{
  digitalWrite(LEDpin, HIGH)  //statement is executed if the x>10 expression is true
}

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. Comparison operators are following:

  • == (equal to) -
  • != (not equal to) -
  • < (less than) -
  • < = (less than or equal to)
  • > (greater than) -
  • > = (greater than or equal to) -

Boolean operators

The Boolean logical operators

  • ! (logical not) -
  • && (logical and) -
  • || (logical or) -

Switch

Switch…case

en/iot-open/programming_fundamentals_rtu/program_control_structures.1516282165.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
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