How to understand the following commands

Hi to all,
I´m learning how to code for arduino and I found in a sketch the following line:
Serial.print(rotationValue < 1 ? "L" : "R");

rotationValue is only a int variable. I can´t understand the other commands, < 1 ? "L" : "R".

L or R are only symbols to be printed, but the meanig of the command is completely nonsense to me. It works but I don´t know why! Could someone help me?
Regards to all
Alfredo

Welcome to the forum

Serial.print(rotationValue < 1 ? "L" : "R");

Is equivalent to

//if (rotationValue) < 1)
if (rotationValue < 1)  //edited to remove extra parenthesis

{
  Serial.print("L");
}
else
{
  Serial.print("R");
}

Google C ternary operator for details

Hello armochim

Take a search engine of your choice and ask the WWW for 'ternary operators +arduino' to collect some data to be sorted out to get the needed information.

Thanks all. I´ve never heard about ternary operators. Time to study more. Thanks a lot for the help!!!

This is a powerful operator to get the code small and more readable.

It makes the source code smaller but not the executable and is much less readable than the expanded version

Only the programmer can decide whether it is worth using

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.