User Tools

Site Tools


language_-_structure

If you want to send us your comments, please do so. Thanks
More on comments


Arduino Language Structure

Ternary operator

This can replace an if then else statement

somevariable = (condition) ? ifexpressionTrueDo : ifexpressionFalseDo;

condition is evaluated. If the result is true (not zero), then ifexpressionTrueDo is evaluated and ifexpressionFalseDo is ignored. In the other case it is the otherway arround
Example, if there is no result then use something like

(counter < 23) ? digitalWrite(LED_BUILTIN, HIGH) : digitalWrite(LED_BUILTIN, LOW) ;

else use something like

bool result = (counter < 23) ? HIGH : LOW;

result will be 1 or 0

Range based for loop

This is C++ code not found in the Arduino IDE but it works

for ( range_declaration : range_expression ) {
  loop_statement
}

This executes a for loop over a range such as all elements in an array.

range_declarationvariable declaration (e.g. auto somevariable or int someothervariable)
range_expressiona range like a brached-list or an array
loop_statementThe compound sequence of instructions the for loop has to execute

Example code

const char pins[] = {2,3,4,5,6,7,8,9};
for(setpin : pins) {
  pinMode(setpin, OUTPUT);
}

Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
language_-_structure.txt · Last modified: 26-04-2020 14:41 by wim