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/23 12:48] Agrisniken:iot-open:programming_fundamentals_rtu:program_control_structures [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 120: Line 120:
  
 ===== Switch case statement ===== ===== 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.+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> <code c>
 switch (x) {  switch (x) { 
    case 0:  //executes when the value of x is 0    case 0:  //executes when the value of x is 0
    // statements    // statements
-   break;+   break;   //goes out of the switch statement
  
    case 1:  //executes when the value of x is 1    case 1:  //executes when the value of x is 1
    // statements    // statements
-   break;+   break;   //goes out of the switch statement
  
    default:  //executes when none of the cases above is true    default:  //executes when none of the cases above is true
    // statements    // statements
-   break;+   break;   //goes out of the switch statement
 } }
 </code> </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.1516711717.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