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);
 }
Erik
  • 261
  • 1
  • 12
  • Arduino uses C++ programming language with specific libraries and .ino file conversion to C++ http://www.cplusplus.com/doc/tutorial/ – Juraj May 02 '18 at 12:49

2 Answers2

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);
}
Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56
  • 2
    Just for fun of it, even the printing if/else can be much shortened: lcd.print(" X"[!!pressed[i]]); – wondra May 02 '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 02 '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 02 '18 at 13:49
  • True, in the organization I work for the ternary operation is allowed, but only in trivial cases like that, if bigger expressiosn are taken into account the if/else should be used. Btw; to make it shorter you can probably even put everything in the for statement (not loop). – Michel Keijzers May 02 '18 at 13:51
  • 1
    It's actually called the "Conditional Operator". It just so happens to be the only ternary operator. – Alexander May 02 '18 at 16:20
  • @wondra I don't know arduino, but if this is really C++ like another comment said then you'd be passing a char instead of a string to lcd.print(). It's not a problem if lcd.print is a polymorphic method that is also made to work with chars, but that's not necessarily the case, so that could be a bug. Strive not to write simple ideas in a complex manner, but to write complex ideas in a simple manner. – JoL May 02 '18 at 17:54
  • Small nit-pick: shouldn't it be equal to saying if (pressed[i] == 1), not if (pressed[i] != 0)? – MCMastery May 02 '18 at 18:44
  • 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 02 '18 at 19:04
  • @MichelKeijzers Ah, sorry. I thought the convention was the other way around for some reason – MCMastery May 02 '18 at 20:34
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
gabonator
  • 371
  • 1
  • 4
  • 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 02 '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 02 '18 at 16:22
  • @Alexander I agree: inline for inline, curly braces for indenting/multiple lines – ke4ukz May 02 '18 at 16:42