This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:program_structures [2023/06/25 16:59] – created ktokarz | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:program_structures [2023/11/23 10:20] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Program Control Statements ==== | + | ====== Program Control Statements, Logical operators |
+ | {{: | ||
+ | It is essential to understand that if no statements change the normal program flow, the microcontroller executes instructions one by one in the order they appear in the source code (from the top - to the down direction). Control statements modify normal program flow by skipping or repeating parts of the code. Often, to decide if the part of the code should be executed or to choose one of the number of possible execution paths, conditional statements are used. For repeating the part of the code, loop statements can be used. | ||
- | It is important to understand that, if there are no statements that change the normal program flow, the microcontroller executes instructions one by one in the order they appear in the source code (from top - to down direction). Control statements make modifications to normal program flow skipping or repeating parts of the code. Often to decide if the part of the code should be executed or not, or to choose one of the number of possible execution paths the conditional statements are used. For repeating the part of the code loop statements can be used. | + | ==== Conditional Statement |
- | + | //**if**// is a statement that checks the condition and executes the following statement if the condition is //TRUE//. There are multiple ways to write down the //**if**// statement: | |
- | === Conditional Statement === | + | |
- | //**if**// is a statement that checks the condition and executes the following statement if the condition is //TRUE//. There are multiple ways how to write down the //if// statement: | + | |
<code c> | <code c> | ||
Line 24: | Line 24: | ||
</ | </ | ||
- | The version with curly braces is used when there is a need to execute part of the code that consists of more than a single statement. Many statements taken together with pair of curly braces are treated as a single statement in such cases. | + | The version with curly braces is used when there is a need to execute part of the code that consists of more than a single statement. Many statements taken together with a pair of curly braces are treated as a single statement in such cases. |
- | When both //TRUE// and //FALSE// cases of the condition should be viewed, the // | + | When both //TRUE// and //FALSE// cases of the condition should be viewed, the // |
<code c> | <code c> | ||
Line 36: | Line 36: | ||
</ | </ | ||
- | If more conditions should be viewed, the //**else if**// part is added to the //if// statement: | + | If more conditions should be viewed, the //**else if**// part is added to the //**if**// statement: |
<code c> | <code c> | ||
if (condition1) { | if (condition1) { | ||
Line 49: | Line 49: | ||
</ | </ | ||
- | The example when the //x// variable is compared and in the cases when it is higher than 10, the // | + | For example, when the //x// variable is compared and when it is higher than 10, the // |
<code c> | <code c> | ||
if (x> | if (x> | ||
Line 58: | Line 58: | ||
</ | </ | ||
- | + | ==== Logical Operators | |
- | === Logical Operators === | + | To allow checking different conditions, logical operators are widely used with the condition statement //**if**// described above. |
- | To allow checking different conditions logical operators are widely used together | + | |
**Comparison Operators** | **Comparison Operators** | ||
- | There are multiple comparison operators used for comparing variables and values. All of these operators compare the value of the variable | + | There are multiple comparison operators used for comparing variables and values. All of these operators compare the variable' |
- | * == (equal to) – if they are equal, the result is //TRUE//, otherwise //FALSE//; | + | * == (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//; | + | * != (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) – 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//; | + | * < = (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) – 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//. | * > = (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//. | ||
Line 102: | Line 101: | ||
The Boolean logical operators in C/C++ are the following: | The Boolean logical operators in C/C++ 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 //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 | + | * && (logical //AND//) – the result is //TRUE// when both operands on the operator' |
- | * || (logical //OR//) – the result is //TRUE// when at least one of the operands on the left and on the right of the operator | + | * || (logical //OR//) – the result is //TRUE// when at least one of the operands on the operator' |
Examples: | Examples: | ||
Line 122: | Line 121: | ||
//Logical OR | //Logical OR | ||
//The statement inside if will execute when at least one of the | //The statement inside if will execute when at least one of the | ||
- | //a and b values | + | //a and b values |
if (a || b){ | if (a || b){ | ||
//Statement | //Statement | ||
Line 128: | Line 127: | ||
</ | </ | ||
- | === Switch Case Statement === | + | ==== Switch Case Statement |
- | Switch | + | A switch |
Examples: | Examples: | ||
Line 148: | Line 147: | ||
</ | </ | ||
- | |||
- | **Check Yourself** | ||
- | |||
- | 1. Which code part is the correct one? | ||
- | |||
- | * 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 //switch// structure should be used? |