Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:program_structures [2023/07/13 11:06] – external edit (Unknown date) 127.0.0.1en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:program_structures [2023/11/23 10:20] (current) pczekalski
Line 1: Line 1:
-==== Program Control Statements, Logical operators ====+====== Program Control Statements, Logical operators ====== 
 +{{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ 
 +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 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:
-=== 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:
 </code> </code>
  
-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 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 //**else**// part should be added to the //**if**// statement in the following ways: When both //TRUE// and //FALSE// cases of the condition should be viewed, the //**else**// part should be added to the //**if**// statement in the following ways:
  
Line 58: Line 58:
 </code> </code>
  
- +==== 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 with the condition statement //**if**// described above.
  
Line 65: Line 64:
  
 There are multiple comparison operators used for comparing variables and values. All of these operators compare the variable's value on the left to the value on the right. Comparison operators are the following: There are multiple comparison operators used for comparing variables and values. All of these operators compare the variable's value on the left to the value on the right. Comparison operators are the following:
-  * == (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 operator's left and right are //TRUE//. If even one of them is //FALSE// the result is //FALSE//; +  * && (logical //AND//) – the result is //TRUE// when both operands on the operator's left and right are //TRUE//. If even one of them is //FALSE// the result is //FALSE//, 
-  * || (logical //OR//) – the 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//.+  * || (logical //OR//) – the result is //TRUE// when at least one of the operands on the operator'left and right is //TRUE//. If both of them are //FALSE//, the result is //FALSE//.
  
 Examples: Examples:
Line 128: Line 127:
 </code> </code>
  
-=== Switch Case 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. 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.
  
Line 148: Line 147:
 </code> </code>
  
- 
-**Check Yourself** 
- 
-1. Which code part is the correct one? 
- 
-  * if(value == 1) digitalWrite(13, HIGH) 
-  * if (value == 1); digitalWrite(13, HIGH) 
-  * if (value == 1) DigitalRead(13,1) 
-2. What is the output of the next code part? 
- 
-<code c> 
-int x = 0; 
-  
-    switch(x) 
-    { 
-  
-      case 1: cout << "One"; 
-  
-      case 0: cout << "Two"; 
-  
-      case 2: cout << "Hello, world!"; 
-  
-    } 
-    </code> 
-3. In which case should the//switch// structure be used? 
en/iot-open/introductiontoembeddedprogramming2/cppfundamentals/program_structures.1689246382.txt.gz · Last modified: 2023/07/13 11:06 by 127.0.0.1
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