Pages: [1]   Go Down
Author Topic: What does the "?" and ":" do in this Code  (Read 281 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a variation of a blink with out delay .

Code:
 ms = millis();

  if (ms - msLast > (relayState ? Relay_ON : Relay_OFF))
  {
    digitalWrite(Relay, relayState = !relayState);
    msLast = ms;
  }
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6367
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://forum.arduino.cc/index.php?topic=201554.0
"Ternary operator"; a sort-of variation of an if/else statement that has a value.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
relayState ? Relay_ON : Relay_OFF

if (relayState) then use Relay_ON otherwise use Relay_OFF

Useful shortcut once you get used to using it.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Cincinnati, OH
Offline Offline
Sr. Member
****
Karma: 15
Posts: 456
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you wrote a simple if statement like:

Code:
if (val == 10) {
   x = 20;
} else {
   x = 15;
}
You could write the same code using the ternary operator as:

Code:
x = (val == 10) ? 20 : 15;

If the conditional expression (val == 10) is True, the expression following the question mark is evaluated. If the conditional expression is False, the expression following the colon is evaluated.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It can also be used as a general if/else as well, discarding any return value (if any):
Code:
(val > 4) ? do_something() : do_something_else();
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Edison Member
*
Karma: 20
Posts: 2104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you wrote a simple if statement like:

Code:
if (val == 10) {
   x = 20;
} else {
   x = 15;
}
You could write the same code using the ternary operator as:

Code:
x = (val == 10) ? 20 : 15;

If the conditional expression (val == 10) is True, the expression following the question mark is evaluated. If the conditional expression is False, the expression following the colon is evaluated.

This is where I'd have gone ...

"No, Mr Engineer Sir.... NO NO NO NO NO NOOOOOO"

Code:
if (x ==10) {?=20 else 15};

would make more sense...
Logged

Pottstown, PA
Offline Offline
Sr. Member
****
Karma: 4
Posts: 297
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for posting this. I put it in my 'Cool Code" folder. The part of the code I still don't understand is the > operator.

Code:
if (ms - msLast > (relayState ? Relay_ON : Relay_OFF))

Just what is the result of 'ms-msLast' being compared to?

- Scotty
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24319
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Just what is the result of 'ms-msLast' being compared to?
Either "Relay_ON" or "Relay_OFF", depending on the value of "relayState"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the replies, it makes more since now.
The ms is milli seconds
The ms Last is milli seconds last

Just like blink without delay.

This way I can use conditions to vary the on and off duration and the on and off don't have to be the same duration like blink with out delay.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Really cjdelphi?

How do I know what variable gets assigned to in your syntax?
What if you want to assign to a variable that doesn't show up anywhere else in the expression?

i.e.
Code:
y = (x>10)?(30):(40);

Your syntax is the one that makes no sense.
Logged

Pages: [1]   Go Up
Jump to: