This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:programming_fundamentals_rtu:program_control_structures [2018/01/18 12:21] – Agrisnik | en:iot-open:programming_fundamentals_rtu:program_control_structures [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Program control structures ====== | ====== 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: | ||
| + | |||
| + | <code c> | ||
| + | //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: | ||
| + | |||
| + | <code c> | ||
| + | if (condition) { | ||
| + | statement1; | ||
| + | } | ||
| + | else { | ||
| + | statement2; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | If more conditions should be viewed, the **else if** part is added to the //if// statement: | ||
| + | <code c> | ||
| + | if (condition1) { | ||
| + | statement1; | ||
| + | } | ||
| + | else if (condition2) { | ||
| + | statement2; | ||
| + | } | ||
| + | else { | ||
| + | statement3; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The example when the //x// variable is compared and in the cases when it is higher than //10//, the // | ||
| + | <code c> | ||
| + | if (x> | ||
| + | { | ||
| + | digitalWrite(LEDpin, | ||
| + | } | ||
| + | </ | ||
| + | |||
| ===== Logical operators ===== | ===== Logical operators ===== | ||
| + | Logical operators are widely used together with the condition operator //if// that is described below. | ||
| ==== Comparison operators ==== | ==== Comparison operators ==== | ||
| - | There are multiple comparison operators used for comparing variables and values. | + | There are multiple comparison operators used for comparing variables and values. |
| - | * == (equal to) - | + | * == (equal to) - if they are equal, the result is //TRUE//, otherwise //FALSE//. |
| - | * != (not equal to) - | + | * != (not equal to) - if they are not equal, the result is //TRUE//, otherwise //FALSE//. |
| - | * < (less than) - | + | * < (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) | + | * < = (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) - | + | * > (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) - | + | * > = (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 // |
| + | |||
| + | Examples: | ||
| + | <code c> | ||
| + | if (x==y){ //equal | ||
| + | // | ||
| + | } | ||
| + | |||
| + | if (x!=y){ //not equal | ||
| + | // | ||
| + | } | ||
| + | |||
| + | if (x<y){ //less than | ||
| + | // | ||
| + | } | ||
| + | |||
| + | if (x<=y){ //less than or equal | ||
| + | // | ||
| + | } | ||
| + | |||
| + | if (x>y){ //greater than | ||
| + | // | ||
| + | } | ||
| + | |||
| + | if (x>=y){ //greater than or equal | ||
| + | // | ||
| + | } | ||
| + | </ | ||
| ==== Boolean operators ==== | ==== Boolean operators ==== | ||
| + | Three Boolean logical operators in the Arduino environment are 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 the 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) - 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: | ||
| + | <code c> | ||
| + | //logical NOT | ||
| + | if (!a) { //the statement inside " | ||
| + | b = !a; //the reverse logical value of " | ||
| + | } | ||
| - | * ! (logical | + | //logical |
| - | | + | if (a && |
| - | | + | |
| + | } | ||
| + | //logical OR | ||
| + | if (a || b){ //the statement inside " | ||
| + | //statement | ||
| + | } | ||
| + | </ | ||
| - | ===== Condition operator | + | ===== Switch case statement |
| - | If...else | + | 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: | ||
| + | <code c> | ||
| + | switch (x) { | ||
| + | case 0: //executes when the value of x is 0 | ||
| + | // statements | ||
| + | | ||
| - | ===== Switch ===== | + | case 1: //executes when the value of x is 1 |
| - | Switch...case | + | // statements |
| + | | ||
| + | | ||
| + | // statements | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | **Check yourself** | ||
| + | 1. Which code part is correct? | ||
| + | * if (value == 1) digitalWrite(13, | ||
| + | * if (value == 1); digitalWrite(13, | ||
| + | * if (value == 1) DigitalRead(13, | ||
| + | 2. What is the output of the next code part? | ||
| + | <code c> | ||
| + | int x = 0; | ||
| + | |||
| + | switch(x) | ||
| + | { | ||
| + | |||
| + | case 1: cout << " | ||
| + | |||
| + | case 0: cout << " | ||
| + | |||
| + | case 2: cout << " | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | 3. In which cases ' | ||