This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:program_structures [2023/11/17 16:32] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:program_structures [2023/11/23 12:20] (current) – pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== 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. | ||
| + | |||
| + | ==== 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: | ||
| + | |||
| + | <code c> | ||
| + | //1st example | ||
| + | if (condition) statement; | ||
| + | |||
| + | //2nd example | ||
| + | if (condition) | ||
| + | statement; | ||
| + | |||
| + | //3rd example | ||
| + | if (condition) { statement; } | ||
| + | |||
| + | //4th example | ||
| + | if (condition) | ||
| + | { | ||
| + | statement; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 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 // | ||
| + | |||
| + | <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; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | For example, when the //x// variable is compared and when it is higher than 10, the // | ||
| + | <code c> | ||
| + | if (x> | ||
| + | { | ||
| + | //Statement is executed if the x > 10 expression is true | ||
| + | digitalWrite(LEDpin, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Logical Operators ==== | ||
| + | To allow checking different conditions, logical operators are widely used with the condition statement //**if**// described above. | ||
| + | |||
| + | **Comparison Operators** | ||
| + | |||
| + | 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//, | ||
| + | * != (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: | ||
| + | <code c> | ||
| + | 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** | ||
| + | |||
| + | 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 //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 operator' | ||
| + | |||
| + | Examples: | ||
| + | <code c> | ||
| + | //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 are TRUE | ||
| + | if (a || b){ | ||
| + | //Statement | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Switch Case Statement ==== | ||
| + | A switch statement similar to the //if// statement controls the flow of a 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 | ||
| + | | ||
| + | } | ||
| + | </ | ||