|
1 | | ---- |
2 | | -title: else |
3 | | -categories: [ "Structure" ] |
4 | | -subCategories: [ "Control Structure" ] |
5 | | ---- |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | -= if...else |
12 | | - |
13 | | - |
14 | | -// OVERVIEW SECTION STARTS |
15 | | -[#overview] |
16 | | --- |
17 | | - |
18 | | -[float] |
19 | | -=== Description |
20 | | -The `if...else` allows greater control over the flow of code than the basic link:../if[if] statement, by allowing multiple tests to be grouped together. An `else` clause (if at all exists) will be executed if the condition in the `if` statement results in `false`. The `else` can proceed another `if` test, so that multiple, mutually exclusive tests can be run at the same time. |
21 | | -[%hardbreaks] |
22 | | - |
23 | | -Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default `else` block is executed, if one is present, and sets the default behavior. |
24 | | -[%hardbreaks] |
25 | | - |
26 | | -Note that an `else if` block may be used with or without a terminating `else` block and vice versa. An unlimited number of such `else if` branches is allowed. |
27 | | - |
28 | | -[float] |
29 | | -=== Syntax |
30 | | -[source,arduino] |
31 | | ----- |
32 | | -if (condition1) { |
33 | | - // do Thing A |
34 | | -} |
35 | | -else if (condition2) { |
36 | | - // do Thing B |
37 | | -} |
38 | | -else { |
39 | | - // do Thing C |
40 | | -} |
41 | | ----- |
42 | | - |
43 | | --- |
44 | | -// OVERVIEW SECTION ENDS |
45 | | - |
46 | | - |
47 | | - |
48 | | -// HOW TO USE SECTION STARTS |
49 | | -[#howtouse] |
50 | | --- |
51 | | -[float] |
52 | | -=== Example Code |
53 | | -Below is an extract from a code for temperature sensor system |
54 | | -[source,arduino] |
55 | | ----- |
56 | | -if (temperature >= 70) { |
57 | | - //Danger! Shut down the system |
58 | | -} |
59 | | -else if (temperature >= 60 && temperature < 70) { |
60 | | - //Warning! User attention required |
61 | | -} |
62 | | -else { |
63 | | - //Safe! Continue usual tasks... |
64 | | -} |
65 | | ----- |
66 | | - |
67 | | --- |
68 | | -// HOW TO USE SECTION ENDS |
69 | | - |
70 | | - |
71 | | - |
72 | | -// SEE ALSO SECTION BEGINS |
73 | | -[#see_also] |
74 | | --- |
75 | | - |
76 | | -[float] |
77 | | -=== See also |
78 | | - |
79 | | -[role="language"] |
80 | | - |
81 | | --- |
82 | | -// SEE ALSO SECTION ENDS |
| 1 | +--- |
| 2 | +title: else |
| 3 | +categories: [ "Structure" ] |
| 4 | +subCategories: [ "Control Structure" ] |
| 5 | +--- |
| 6 | + |
| 7 | += if...else |
| 8 | + |
| 9 | + |
| 10 | +// OVERVIEW SECTION STARTS |
| 11 | +[#overview] |
| 12 | +-- |
| 13 | + |
| 14 | +[float] |
| 15 | +=== Description |
| 16 | +The `if...else` allows greater control over the flow of code than the basic link:../if[if] statement, by allowing multiple tests to be grouped together. An `else` clause (if at all exists) will be executed if the condition in the `if` statement results in `false`. The `else` can proceed another `if` test, so that multiple, mutually exclusive tests can be run at the same time. |
| 17 | +[%hardbreaks] |
| 18 | + |
| 19 | +Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default `else` block is executed, if one is present, and sets the default behavior. |
| 20 | +[%hardbreaks] |
| 21 | + |
| 22 | +Note that an `else if` block may be used with or without a terminating `else` block and vice versa. An unlimited number of such `else if` branches is allowed. |
| 23 | + |
| 24 | +[float] |
| 25 | +=== Syntax |
| 26 | +[source,arduino] |
| 27 | +---- |
| 28 | +if (condition1) { |
| 29 | + // do Thing A |
| 30 | +} |
| 31 | +else if (condition2) { |
| 32 | + // do Thing B |
| 33 | +} |
| 34 | +else { |
| 35 | + // do Thing C |
| 36 | +} |
| 37 | +---- |
| 38 | + |
| 39 | +-- |
| 40 | +// OVERVIEW SECTION ENDS |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +// HOW TO USE SECTION STARTS |
| 45 | +[#howtouse] |
| 46 | +-- |
| 47 | +[float] |
| 48 | +=== Example Code |
| 49 | +Below is an extract from a code for temperature sensor system |
| 50 | +[source,arduino] |
| 51 | +---- |
| 52 | +if (temperature >= 70) |
| 53 | +{ |
| 54 | + // Danger! Shut down the system |
| 55 | +} |
| 56 | +else if (temperature >= 60) // 60 <= temperature < 70 |
| 57 | +{ |
| 58 | + // Warning! User attention required |
| 59 | +} |
| 60 | +else // temperature < 60 |
| 61 | +{ |
| 62 | + // Safe! Continue usual tasks... |
| 63 | +} |
| 64 | +---- |
| 65 | + |
| 66 | +-- |
| 67 | +// HOW TO USE SECTION ENDS |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +// SEE ALSO SECTION BEGINS |
| 72 | +[#see_also] |
| 73 | +-- |
| 74 | + |
| 75 | +[float] |
| 76 | +=== See also |
| 77 | + |
| 78 | +[role="language"] |
| 79 | + |
| 80 | +-- |
| 81 | +// SEE ALSO SECTION ENDS |
0 commit comments