I can't find ":" and "?" in the Arduino Language Reference.
What do they mean in the following command line?
**PastB ? encoder0Pos++ : encoder0Pos--;**
Sorry for stupid Questions and Thanks for a short answer
Btw: The Encoder - Programm example, found on the official Arduino Side, is working fine.
if (PastB != 0)
encoder0Pos++;
else
encoder0Pos--;
Google search "C++ ternary operator"
Have you heard the name of ternary (?: ) operator? Make a google search writing ternary operator.
Yeah, the problem with that is, you have to know in advance that it is called the ternary operator.
Google freaks out on the '?' character.
J-M-L
June 8, 2022, 5:09pm
7
Looking for c++ :? finds the right answer in the very first hits and c++ ":?" gets you there right away
Of course you need to k now it’s C++ and not the arduino language
Thanks a lot, AsAWOL!
This makes sense!
I'll change this line to "if and else"
Unfortunately I never heard "ternary". I have never learned any Programming Language properly.
Greetings from Germany to all who answered my question.
J-M-L
June 8, 2022, 5:11pm
9
May be you should read a tutorial
Have you heard this name: Conditional Operator?
Thank You Mostafa!
You understand, Arduino and Arduino IDE are perfect for those who have no glue and will never have. Maybe the greatest invention of the last 20 Years!
For my Projekts I rely on the Language Reference and Examples of the official Arduino Side with i find very effective. To learn C++ overnight my lifetime is to short.
Thanks again for Your kind assistance.
Today I learned something!
(Sorry for poor English)
Try it and you will have a lot of fun to code more practically.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
J-M-L
June 8, 2022, 6:03pm
13
you can find some in almost any store
If PastB is true , then the variable encoder0Pos is incremented ; else, the variable encoder0Pos is decremented
===>
if(PastB == 1)
encoder0Pos++;
else
encoder0Pos--;
It compiles exactly the same, so why use the Ternary Operator? For programmers to show off.
J-M-L
June 8, 2022, 9:00pm
16
Well if that is the whole line of code then they don’t show off much… it’s not the typical use case, you find that more in an assignment expression
Koepel
June 8, 2022, 9:47pm
17
It can be useful to convert a 'bool' into a HIGH / LOW in a short and simple way:
bool blinkState = false;
...
blinkState = !blinkState;
digitalWrite(pin, blinkState ? HIGH : LOW);
Good point, IIRC it can handle expressions, not just statements.
Not all answers HAVE to be a "look for yourself" reply.
Just answer as #2 does and save a lot of unnecessary back and forth BS.
Hello tantalelko
The ternary operator , ? and :, supports the human thinking during coding in C++ simply.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!