small medium large xlarge

Back to: All Forums  Arduino
18 Jul 2011, 12:13
Pierpaolo Placenza (2 posts)

Hi all. First of all excuse me for my poor English, I learned by myself. I’m writing for a little syntax problem in binary dice code I found on this book (I like a loot book because is simple and clear, fantastic for beginners … like me).

I found following row that I don’t understand syntax (page 61): led_state=(led_state)==LOW) ? HIGH : LOW;

with == I assign a value to led_state variable, but I didn’t understand meaning of second part: ? HIGH : LOW

Can you explain me. Thanks in advance. Bye

18 Jul 2011, 16:35
Maik Schmidt (120 posts)

Hi Pierpaolo!

The statement

@led_state = (led_state == LOW) ? HIGH : LOW;@

is a shortcut for

  if (led_state == LOW)

    led_state = HIGH;


    led_state = LOW;

It is called ternary operator and is part of the C programming language.

Please, note that with ~~~ == ~~~ you do not assign values! It is a comparison operator and checks, if two values are equal. Assignment is done using =.

Hope this helps!


18 Jul 2011, 17:25
Pierpaolo Placenza (2 posts)

Hi Maik. Thanks a lot. I’m not an expert and I didn’t find a guide related to this topic. It was very helpful for me your explanation. I found your book very easy and clear. It arrived at my office only friday (I live in Italy) and I read about 100 pages!.

Thanks again. Bye

You must be logged in to comment