5

I recently came across this code in a tutorial. The code works, but this form of syntax seems to differ quite a bit from https://www.arduino.cc/reference/en/language/structure/control-structure/if/, as it's lacking comparison operators and curly braces for statements. Can someone please run down why this works?

for (int i = 0; i < 15; i++)
{
lcd.setCursor(i, 1);
if (pressed[i])
  lcd.print("X");
else
  lcd.print(" ");

if (justpressed[i])
  digitalWrite(led, HIGH);
else
  digitalWrite(led, LOW);
 }
6

About the missing conditions, e.g.:

if (pressed[i])

If is supposed to be a boolean, this is the prefered way, as long as the boolean has value zero (0) when false and nonzero when true. It is the same as writing:

if (pressed[i] != 0)

It is not usual to do this for integers, only for booleans.

About the brackets: If you have only one statement the brackets can be omitted. But it is quite a bad programming practice, since sooner or later you add a statement, without the brackets added and you have a bug introduced.

If you really want to shorten it than instead of

if (justpressed[i])
  digitalWrite(led, HIGH);
else
  digitalWrite(led, LOW);

use

digitalWrite(justpressed[i] ? HIGH : LOW);

This is called the Ternary operator.

update

Using the remark of wondra but with the ternary operation your code can be

for (int i = 0; i < 15; i++)
{
    lcd.setCursor(i, 1);
    lcd.print   (     pressed    [i] ? "X"  : " ");
    digitalWrite(led, justpressed[i] ? HIGH : LOW);
}
  • 2
    Just for fun of it, even the printing if/else can be much shortened: lcd.print(" X"[!!pressed[i]]); – wondra May 2 '18 at 13:40
  • 1
    @wondra right ... but most have to read a few times to see why it works.. I would it change to lcd.print(pressed[i] ? "X" : " "); probably. – Michel Keijzers May 2 '18 at 13:43
  • 3
    Sadly, fun =/= smart in most cases. Fully agree - if the code is to be ever maintained one should stick to the most straight-forward solution (whatever that is it expected to be for the poor fellow doing the code maintenance). – wondra May 2 '18 at 13:49
  • 1
    It's actually called the "Conditional Operator". It just so happens to be the only ternary operator. – Alexander May 2 '18 at 16:20
  • 1
    @McMastery Actually it's not the same ... there is a convention (not always true, especially in C), that a boolean FALSE value is 0, and a TRUE value is non zero. In your case, if TRUE would be 255 it would go wrong. – Michel Keijzers May 2 '18 at 19:04
4
  1. if (something) is the same as if(something!=0), if condition is a non-zero value, it is considered as fulfilled condition, read about the Conditionals - The true or false story
  2. For single line statements you don't need to use curly braces. But using the braces makes the code more readable and the code is less prone to errors
  • 1
    It's worth noting that curly braces on single-line statements can definitely help prevent some insidious bugs: remember Apple's famous SSL bug? Curly braces almost certainly would have avoided that, or at least made it more clear where the error was. – ke4ukz May 2 '18 at 14:59
  • @ke4ukz I think the fear of repeating that SSL bug is over stated. If you're going to use an inline if statement, do it ... in... line. Don't put it on a new line. if (someEarlyExitCondition()) return; never hurt anyone, if it was all in one line. The new line+indent that people put whenever they use inline if statements always confused me. It's just so unnecessary! – Alexander May 2 '18 at 16:22
  • @Alexander I agree: inline for inline, curly braces for indenting/multiple lines – ke4ukz May 2 '18 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.