I have always used curley brackets around both if and else instructions, as per reference, but I have just noticed that there are none in the if/else part of the blink without delay example.
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
You can resign the brackets if you only have one single instruction to be executed.
It doesn't have any advantages except saving two bracket charactes and in my opinion it's really bad coding style. It's a common cause of defect as you allways run the risk of adding another instruction to be executed in the if/else block but forget to add brackets. Also it makes the code harder to read.
Snippet1 + Snippet2 do the same thing:
// Snippet1
if (ledState == LOW)
ledState = HIGH;
// Snippet2
if (ledState == LOW) {
ledState = HIGH;
}
But if you add another instruction you have to add brackets. Snippet3 + Snippet4 are not doing the same thing.
[OT] One of my favourite "weeding-out" interview questions used to be
"I have a variable that can have the value three or five.
If the value is three, make it five, if it is five, make it three.
Your answer should not contain an "if".
AWOL:
[OT] One of my favourite "weeding-out" interview questions used to be
"I have a variable that can have the value three or five.
If the value is three, make it five, if it is five, make it three.
Your answer should not contain an "if".
switch (var)
{
case 3: var = 5; break;
case 5: var = 3; break;
}
Seriously, just kidding.
var ^= 2;
(edit) In case you don't read page 2, it should be:
Well, thank you very much Mr Gammon, if you'd like to collect your expenses on your way out...
Next candidate, please.
See? Great question for sorting the wheat from the chaff.
Almost as good as the interviewee asked "What sort of range of numbers could you expect to store in an eight bit memory location?"
"Plus or minus 144" came back the answer.
Boffin1:
I have always used curley brackets around both if and else instructions, as per reference, but I have just noticed that there are none in the if/else part of the blink without delay example.
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
Whats the difference / advantage?
Or replace with:
digitalWrite(ledPin, ! digitalRead (ledPin));
That does away with a variable which might not have the value you expect in it.
The lookup table is interesting and feasible, if a little wasteful of RAM.
It will certainly be quicker than a method involving a division operation on a processor that doesn't have a divide instruction.
The XOR is going to be as quick as the subtract method, but only if the operands are correct
Boffin1:
OK so how does that work, and are Mr Gammon's expensive less than my dividing ?
Look at the bit patterns. Also a lot of microprocessors don't have a divide instruction, but almost certainly they have an XOR which is implemented very quickly.
Dividing will be implemented in software, XOR in hardware.