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/23 09:43] – Agrisnik | en:iot-open:programming_fundamentals_rtu:program_control_structures [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 95: | Line 95: | ||
| ==== Boolean operators ==== | ==== Boolean operators ==== | ||
| - | The Boolean logical operators | + | Three Boolean logical operators |
| - | * ! (logical | + | * ! (logical |
| - | * && (logical | + | * && (logical |
| - | * || (logical | + | * || (logical |
| + | Examples: | ||
| + | <code c> | ||
| + | //logical NOT | ||
| + | if (!a) { //the statement inside " | ||
| + | b = !a; //the reverse logical value of " | ||
| + | } | ||
| + | //logical AND | ||
| + | if (a && b){ //the statement inside " | ||
| + | //statement | ||
| + | } | ||
| - | ===== Switch ===== | + | //logical OR |
| - | Switch...case | + | if (a || b){ //the statement inside " |
| + | // | ||
| + | } | ||
| + | </ | ||
| + | ===== 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: | ||
| + | <code c> | ||
| + | switch (x) { | ||
| + | case 0: //executes when the value of x is 0 | ||
| + | // statements | ||
| + | | ||
| + | case 1: //executes when the value of x is 1 | ||
| + | // 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 ' | ||