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:programming_fundamentals_rtu:program_control_structures [2018/01/18 13:37] Agrisniken:iot-open:programming_fundamentals_rtu:program_control_structures [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 46: Line 46:
 </code> </code>
  
 +The example when the //x// variable is compared and in the cases when it is higher than //10//, the //digitalWrite()// method executes. 
 <code c> <code c>
 if (x>10)  if (x>10) 
Line 58: Line 59:
  
 ==== Comparison operators ==== ==== Comparison operators ====
-There are multiple comparison operators used for comparing variables and values. Comparison operators are following: +There are multiple comparison operators used for comparing variables and values. All of these operators compare the value of the variable on the left to the value of the variable on the right. Comparison operators are following: 
-  * == (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 //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 
 +
 +</code>
  
 ==== Boolean operators ==== ==== Boolean operators ====
-The Boolean logical operators +Three Boolean logical operators in the Arduino environment are following: 
-  * ! (logical not) -  +  * ! (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) -  +  * && (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) - +  * || (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 "if" will execute when the "a" is FALSE
 +  b = !a; //the reverse logical value of "a" is assigned to the variable "b"
 +}
  
 +//logical AND
 +if (a && b){  //the statement inside "if" will execute when the values both of the "a" and "b" are TRUE
 +  //statement
 +}
  
-===== Switch ===== +//logical OR 
-Switch...case+if (a || b){  //the statement inside "if" will execute when at least one of the "a" and "b" values is TRUE 
 +  //statement 
 +
 +</code>
  
  
 +===== 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
 +   break;   //goes out of the switch statement
  
 +   case 1:  //executes when the value of x is 1
 +   // statements
 +   break;   //goes out of the switch statement
  
 +   default:  //executes when none of the cases above is true
 +   // statements
 +   break;   //goes out of the switch statement
 +}
 +</code>
 +
 +
 +**Check yourself**
 +1. Which code part is correct?
 +
 +  * 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 cases 'switch structure' should be used?
en/iot-open/programming_fundamentals_rtu/program_control_structures.1516282638.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
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