Need help identifying the origin of this unconventional "if" construction

Hello.

After some time wandering about other subjects, I finally found the time to get back to Arduino assemblies. Now I'm using TinkerCAD in order to previously simulate whatever is possible prior to go protoboarding the final circuit. Anyway, by lurking around other people's contributions at TinkerCAD, I found a single code line in one sketch that called my attention. There is no need to post the whole code, in fact this line could be anywhere in any Arduino code. The code line is as follows:

second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;

"first" and "second" are integer type variables, and "total" is a float-type variable. This line replaces a classical "if...else" statement like this:

if (second == 0) lcd.print("Invalid");
else total = (float)first / (float)second;

I just would like to know what is the origin of the line that works as an "if...else" statement. I tested in some of my sketches and works fine. Is it some C++ well known structure? I'm actually totally unaware of it.

Thanks for any answer.

Do a google search for the c/c++ ternary operator.

by the way that is a cool avatar you are using best regards Stefan

Thanks, david_2018. About that, are there any other C/C++ 'unconventional' structures that I should search for? I wrote 'unconventional' in my point of view, of course, because my knowledge of C/C++ is below average. I actually studied C/C++ in the late 90's and had a refresh in 2001/2002 for personal interest.

StefanL38, thanks for your comments. I'm the one who draw the avatar... actually, kinda "saw" it when looking at the Arduino logo trying to imagine a good avatar based on that logo. In fact, I now have the feeling I do not know where is the original hand-drawing I made then... :cry:

the ?: operator is old as C. so you learned it in 90's

Hi, Juraj. Thanks for your comment.

I must confess I did not get to the point of using this structure until now that I saw it. Chances are the ?: operator was mentioned in some part of the documentation I had at the time and did not check deeply enough. I studied C/C++ as self learning in the 90's, first in Portuguese-written books - some translations, other originally written in Portuguese - but as soon as I could, I changed to English-written docs. Later in 2001/2002 I got back to it by reading new editions, although I still keep my 1,114 pages, 1997 ed, Herbert Schildt's "Borland C++ The Complete Reference" at hand... :astonished:

Cheers.

lcvieira_br:
I still keep my 1,114 pages, 1997 ed, Herbert Schildt's "Borland C++ The Complete Reference" at hand...

.If the ternary operator isn't in that, maybe you should ask for your money back, for not being as complete as it claims.

There’s also the comma operator. Don’t use it.

The "?:" operator is NOT intended to be a substitute for an 'if' statement. It should generally be used only on the right side of an assignment or as a function argument where the 'if' statement can't be used. It is a shortcut for the fairly common:

  if (condition)
    variable = expression;
  else
    variable = otherExpression;

That becomes:

  variable = condition ? expression : otherExpression;

It's like the 'switch/case' statement which is a shortcut for the fairly common:

  if (variable == integerConstant1)
  {
    // do something 1
  }
  else if (variable == integerConstant2 || variable == integerConstant3)
  {
    // do something 2 or 3
  }
else if (variable == integerConstant4)
  {
    // do something 4
  }
else if (variable == integerConstant5)
  {
    // do something 5
  }
  else
  {
    // Do some default thing
  }

That gets simplified to:

  switch (variable)
  case integerConstant1:
    // do something 1
    break;

  case integerConstant2:
  case integerConstant3:
    // do something 2 or 3
    break;

  case integerConstant4:
    // do something 4
    break;

  case integerConstant5:
    // do something 5
    break;

  default:
    // Do some default thing
    break;
  }

Guys, thanks a lot.

FormerAWOL, the book indeed explores the ternary operator, pages 41, 51, 55-57, 684 & 688. I'm the one who let it pass in by... Thanks.

westfw, the comma operator is mentioned in page 44. Indeed, another breed... :o

And jhonwasser, thanks for the enlightening clarification. Indeed, a complete review of the "?:" operator functionality is far better than a narrowed, spotted view. I assure my understanding of it is now fair enough for using with reason... and necessary wisdom.

I know this is an Arduino-driven forum, but since C/C++ are kinda backbone for the Arduino language, mastering it helps mastering Arduino as well. And I am actually writing a code for data logging with the use of EEPROM, SDcard and external digital & analog sensors, with educational purpose. I also plan to go further with a PicoCLP using Arduino MEGA, also with educational purposes... But this one is not for these days, needs further tinkering.

In time: if you could help me a bit more, which part of the Forum is best suited for exploring facts related to the ATMEGA internal EEPROM? Mostly electrical facts? I have read and heard people mentioning that abusive use of the EEPROM might cause problems. :astonished: I'd like to understand what are the actions that might be dangerous and abusive to the EEPROM inside the Arduino's microcontrollers.

Best regards and thanks again.

(I hope my questioning was not out of the purpose of this section... As a foreigner and a visitor, I must take care not trespassing the safety boundaries)

lcvieira_br:
Thanks, david_2018. About that, are there any other C/C++ 'unconventional' structures that I should search for?

Check out "Duff's Device".

Thanks, PaulMurrayCbr. I'll check this out as well.

Cheers.

There is '!!' also ... :slight_smile:

aarg:
There is '!!' also ... :slight_smile:

Hi, "aarg"
I did not find any reference to a "!!" operator... double negate, perhaps? (or double denial)? ::slight_smile:

I'd like to understand what are the actions that might be dangerous and abusive to the EEPROM inside the Arduino's microcontrollers.

The EEPROM is guaranteed for 100,000 erase, write cycles per cell. After that the cell is no longer guaranteed to perform properly.

History side note:

One of the programming magazines I used to read way long ago had a section / contest for obscure code - sometimes awards were given for the most torturing of the Pre Processor(?)

Legend also was that part of the UNIX Kernel had only one comment "you are not supposed to understand this"

I always tried to stay away from real obscure code, but sometimes I could not resist an "elegant" construct,

lcvieira_br:
Hi, "aarg"
I did not find any reference to a "!!" operator... double negate, perhaps? (or double denial)? ::slight_smile:

It's not a single operator, it's two NOT operators, used to turn a non-zero value into a one, and leave a zero value as a zero.

Many will frown on its use.

groundFungus:
The EEPROM is guaranteed for 100,000 erase, write cycles per cell. After that the cell is no longer guaranteed to perform properly.

Thanks for this information. By consulting ATMEL documentation related to the 328-family EEPROM - and the 2560's as well - there are some warnings about power supply stability, use of interruptions, EEPROM data corruption and other related references. I tried this forum's "Storage" section (Using Arduino to interface SD cards, floppy drives, harddrives, EEPROMs, etc) in order to find more about this, meaning: how the Arduino platform handles these issues and if it handles these issues or not; if it handles, how does it happen: by hardware design or software, and so, but I did not succeed. I think I'll post there about his because I tried and searched and found no specific remark. Thanks again.

lcvieira_br:
Thanks for this information. By consulting ATMEL documentation related to the 328-family EEPROM - and the 2560's as well - there are some warnings about power supply stability, use of interruptions, EEPROM data corruption and other related references. I tried this forum's "Storage" section (Using Arduino to interface SD cards, floppy drives, harddrives, EEPROMs, etc) in order to find more about this, meaning: how the Arduino platform handles these issues and if it handles these issues or not; if it handles, how does it happen: by hardware design or software, and so, but I did not succeed. I think I'll post there about his because I tried and searched and found no specific remark. Thanks again.

What is your question, exactly?