用户工具

站点工具


en:reference:language:else
no way to compare when less than two revisions


前一修订版
en:reference:language:else [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
 +====== if / else ======
 +
 +if/else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. For example, an analog input could be tested and one action taken if the input was less than 500, and another action taken if the input was 500 or greater. The code would look like this:
 +
 +<code cpp>
 +if (pinFiveInput < 500)
 +{
 +  // do Thing A
 +}
 +else
 +{
 +  // do Thing B
 +}
 +</code>
 +
 +else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time.
 +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.
 +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.
 +
 +<code cpp>
 +if (pinFiveInput < 500)
 +{
 +  // do Thing A
 +}
 +else if (pinFiveInput >= 1000)
 +{
 +  // do Thing B
 +}
 +else
 +{
 +  // do Thing C
 +}
 +</code>
 +
 +Another way to express branching, mutually exclusive tests, is with the  [[reference:language:switchcase|switch case]] statement.
 +
  
en/reference/language/else.txt · 最后更改: 2023/06/07 04:23 由 127.0.0.1