If...else

From Microduino Wiki
Jump to: navigation, search
  • if / else

if/else a flow control statement which is more advance than if. Multiple condition tests can be run. For example, testing the value of analog input, what should be executed when it is less than 500, and what should be executed when it is greater or equal to 500. The code is as following:


if (pinFiveInput < 500)
{
  //  Execute operation A
}
else
{
  //  Execute operation B
}

Else can execute additional test, so multiple mutexes conditions can be tested at the same time.

The test will be operated one by one, until some test is true, at this point the test execution block will be run, and the rest tests will be skipped, and execute the next statement of if/else directly. When all tests are false, if there is a else statement block, it will execute the default else statement block.

Pay attention to that the else if statement block may haven’t a else statement block. The number of else if branch statement is not limited.


if (pinFiveInput < 500)
{
  //  Execute operation A
}
else if (pinFiveInput >= 1000)
{
  // Execute operation B
}
else
{
  //  Execute operation C
}

Another multi-condition branch judgment statement is switch case.


[Return to Arduino Syntax Manual]