# What does this mean " int dirx=dx>0?1:-1;"

I'm sure the answer to this is quite simple but I don't have a clue. Can someone help, please?

The "?" is known as the ternary operator or conditional operator. The code is equivalent to

``````int dirx;
if (dx > 0)
{
dirx = 1;
}
else
{
dirx = -1;
}
``````

http://www.cplusplus.com/forum/articles/14631/

Brilliant! Thank you so much. C++ could as well be Greek at some points. But your explanation solves my problem with a sketch I can't get to work. Thank you again

Actually, the ternary operator involves three operand expressions and two operators:

exp1 ? exp2 : exp3

where the two operators are the '?' and the ':' Usually, exp1 is a conditional expression that resolves to a logic true or false state. If exp1 is logic true, exp2 is evaluated. If exp1 is logic false, exp3 is evaluated. As Hackscribble pointed out, it can be used to replace a simple if-else statement block:

``````   if (exp1) {
exp2;
} else {
exp3;
}
``````

OK. I feel I have the measure of this statement now, but my book (note only one!!) doesn't even mention ternary operators. I thought that Arduino sketches would use the simple side of C++. But thanks for the help.

It is a useful bit of shorthand. For example, if you need to print the words "adult" or "child" depending on the result of an age comparison, you can do this:

``````Serial.print("Based on your age, you are ");
Serial.println(yourAge >= 18 ? "an adult" : "a child");
``````

OK. I feel I have the measure of this statement now, but my book (note only one!!) doesn't even mention ternary operators. I thought that Arduino sketches would use the simple side of C++. But thanks for the help.

treereaver: OK. I feel I have the measure of this statement now, but my book (note only one!!) doesn't even mention ternary operators. I thought that Arduino sketches would use the simple side of C++. But thanks for the help.

Well, it is a ternary operator because it accepts three operands, however its name is the conditional operator.

While an if statement can be replaced with a conditional operator, doing the reverse can be less efficient. What most articles are looking over is the fact a conditional operator returns the type of expression put into it.

(a ? b : c )++; (mode ? analogRead : digitalRead)( A0 ); (a ? b : c) = (d ? e : f );

Here is my article covering basically what I've just mentioned.

treereaver: I thought that Arduino sketches would use the simple side of C++.

It is full C++ minus the exception handling. And the STL is not supplied by default.

Thanks Nick. What I was trying to say was that I had hoped Arduino sketches would use the simpletons side of C++, so that I wouldn't need to learn to walk before I learnt to run. My real problem is that the whole Arduino thing is so much fun it's overwhelming my level of competence. What's STL?

Standard Template Library

No one is forcing you to use it.

treereaver: Thanks Nick. What I was trying to say was that I had hoped Arduino sketches would use the simpletons side of C++, so that I wouldn't need to learn to walk before I learnt to run.

You only have to use as much as you are comfortable with.

Thanks Nick. I was comfortable at the blink LED level. Got kinda hung up with turning RAW accelerometer data into useable output so my R/C helicopter will auto hover, something my Royal Navy SAR Wessex wouldn't do, and fell totally foul of the really complex ternary stuff to drive a 3 axis CNC 3d carving machine. The comfort zone is now well stretched.

AWOL - Standard Template Library? What is that and do I need to know in order to use the Arduino IDE.

Michinyon - Yes they are! I can't program an arduino in Pascal, and I won't give a Raspberry Pi house room. My original CNC machine ran on HP GL, scraped together from their plotter language. But the computer was a windows 3.1 machine using compiled BASIC. My point is still that the Arduino and its entourage of bits and shields just cries out to be used in many fun as well as serious ways.

Thanks for all the replies. My world ruling robots will gently make their way out of the Evil Genius Laboratory. If only I could corner the world market for AAA batteries!!

Standard Template Library? What is that and do I need to know in order to use the Arduino IDE.

It provides some standard functions for handling things like lists, vectors, queues, maps and suchlike. Being templated it can be applied to many different types (eg. a list of long, a list of int, a list of bytes etc.).

You don't need to know about it, and it is a slight pain to install. I mentioned it for completeness more than anything else.

Thank you again Nick. Even though I now understand what is going on I still can't get this demo sketch to work. That is, however, a job for a different forum. I will however r4ely on the folk in this one for nightmare issues of misunderstanding. Thanks again.