Use of ? to decide which string to use

I recently came across the following: Serial.println(Output ? "On" : "Off"); which, from what appeared when printed showed that what was printed, "On" or "Off, was determined by the value of the variable 'Output'. I have found, by intuition, that I can use, for example: pstring = pstring + String(Direction ? 'C':'T'); to append either 'C' or 'T' to pstring depending on the value of 'Direction'. What I can't find is a description of this, nor the correct 'google' phrase, so I can actually find the definition of the use of '?' in this manner. Any links, or really clear SIMPLE explanations? Thanks Dave

Google "c++ triadic operator" or "c++ ternary operator"

http://www.cplusplus.com/forum/beginner/139650/

The first answer.

A ? B:C Gives B if A is true, C if A is false.

AWOL and CFK, thanks to both of you, so it doesn't just apply to strings!!!! I'm so glad I asked. 40 years of coding, but only 3 weeks in C, and I'm still learning. I started playing with Arduino's to stop my 70 year old brain becoming a vegetable and I'm so glad I did, I notice the difference already. Regards Dave PS I also have the Raspberry Pi but I'm not sure I can cope with C and Python at the same time. :confused:

I'm learning every day still...

Python is also great. But as my old teacher said: "Start with C, everything else becomes so much easier when you already know C!"

I never had a teacher (for coding), everything I know comes from reading and practice practice practice. Starting 1973 ( no internet or IBM PC then) with a PDP8S at home coding on the front panel switches, at work with an IBM1130 using assembly and Fortran V. Then 6502 and 68000 assembly, Pascal and Power Basic (very pascal like). Back in those days I could code without even one bug in 1000+ lines(I'm not joking), today with C it seems to be 1000 bugs per line!!! Keeping track of those {} really freaks me out. Syntax, reserved words etc etc, when you code in assembler there's no syntax, just the logic of what you are doing. How I hate myself for having got rid of my 6502 system, it could have done all that I want to do with my Arduino and more so much more easily. Dave PS God I feel old!!!!

DavidI: Keeping track of those {} really freaks me out.

That's why we indent and have invented smart editors which match braces, if you need them.

Syntax, reserved words etc etc, when you code in assembler there's no syntax

Apart from whether it was immediate, or register, or indexed addressing, post increment/decrement etc.

How I hate myself for having got rid of my 6502 system, it could have done all that I want to do with my Arduino and more so much more easily.

Learn to program FPGAs - the 6502 core is available as a library object, so (mutiple) 100MHz 6502s are a doddle. for some.

Interestingly, I don't think the oft-quoted "10 lines a day" metric has changed much, but you can normally do a lot more with ten lines of intermediate-level language like C, than you ever could with ten lines of asm.

AWOL: That's why we indent and have invented smart editors which match braces, if you need them.

Hm That's why

case '-': case '+': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': //numeric Serial.println(message); messptr = 0; int incr = atoi(message);

if (incr != 0) { // only numbers so move the prior selected servo if (servnum > 0) { moveservo(servnum, last[servnum] += incr); } else { pstring = "No servo selected"; Serial.println(pstring); } } break; gives me Arduino: 1.6.4 (Windows 7), Board: "Arduino Pro or Pro Mini, ATmega328 (5V, 16 MHz)"

multiservo4f.ino: In function 'void loop()': multiservo4f:225: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:226: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:233: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:234: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:242: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:243: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:251: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:252: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:260: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:261: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:276: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:277: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:303: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:304: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:335: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:336: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:362: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:368: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' multiservo4f:377: error: jump to case label [-fpermissive] multiservo4f:212: error: crosses initialization of 'int incr' jump to case label [-fpermissive]

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

but case '7': case '8': case '9': { //numeric Serial.println(message); messptr = 0; int incr = atoi(message);

if (incr != 0) { // only numbers so move the prior selected servo if (servnum > 0) { moveservo(servnum, last[servnum] += incr); } else { pstring = "No servo selected"; Serial.println(pstring); } } break; } doesn't, see addition of a pair of {} and case '6': case '7': case '8': case '9': //numeric Serial.println(message); messptr = 0; int incr; incr = atoi(message);

if (incr != 0) { // only numbers so move the prior selected servo if (servnum > 0) { moveservo(servnum, last[servnum] += incr); } else { pstring = "No servo selected"; Serial.println(pstring); } } break; separating the 'int incr' from 'incr = atoi(message)' doesn't either!

FPGA's are not an option for persons in retirement... unfortunately.

If I could have only coded 10 lines a day of assembler I would have killed myself. 10 lines a day of Pascal is still very much on the low side, that's just a tad over 1 line an hour, ridiculous!

Well, the code's incomplete, so I'm not even going to try compiling it or argue. The missing code tags may also not help.

The 10 lines a day goes back (at least) to "The Mythical Man-month" from the mid 70s.

Keeping track of those {} really freaks me out.

Even with the not so smart editor in the IDE it should not be difficult.

Suggestions. Every time you type a { enter a couple of linefeeds then type the corresponding } immediately and only then enter the code to be executed in the block.

Always put { and } on lines of their own and use Ctrl -T frequently to show the structure of the program.

Always use { and } after if and else even if they could be omitted because only one line of code is to be executed.

Put the insertion point to the right of any { or } and the IDE will indicate the matching { or }

Put the insertion point to the right of any { or } and double click to have the whole code block encompassed by the braces highlighted.

UKHeliBob: Even with the not so smart editor in the IDE it should not be difficult. snip....

Put the insertion point to the right of any { or } and double click to have the whole code block encompassed by the braces highlighted.

Now that bit I didn't know! I found a problem if I deleted a bit of code and inadvertently also deleted a { or }, your suggestion to put them on a line of their own should help me prevent that.

@AWOL the entire code is more than the allowed 9000 bytes for the forum, I've just tried. essentially int incr = atoi(message); doesn't compile unless the complete case block is delineated with a {} pair but int incr; incr =atoi(message); doesn't need that additional {}, which I was told in another 3D that a 'case' block doesn't need, Regards Dave

You can attach a file containing your program if it is too large to post directly. See Attachment and other options bottom/left of the Reply editor. Note - NOT the Quick Reply editor.

As case block does not need braces but the switch block does.

Decided to open a new topic on the use of braces around 'case'.

see http://forum.arduino.cc/index.php?topic=324205.0

Dave

Looks like a cross-post to me.

DavidI:
AWOL and CFK, thanks to both of you, so it doesn’t just apply to strings!!! I’m so glad I asked.

My advice is: use it sparingly. The old: A ? B : C is confusing to look at, even if you have been coding for years. Especially if you are not sure about operator precedence.

Just as an example:

a = b << 3 & 0xF ? c && d == 5 | 6 : b | 6;

Can you be sure this is going to give the results you expect?

[quote author=Nick Gammon date=1432121388 link=msg=2239903] Looks like a cross-post to me. [/quote] I changed it because this thread was on the use of the tertiary operator '?' (if I've got the name right) but had developed into a discussion on the use of braces in case statements.

... when you code in assembler there's no syntax, just the logic of what you are doing ...

Can't agree with that. There is always syntax. And always logic.

For example, in the old 6502:

  LDA  (SRCE),Y   
  CMP  #$C1       
  BLT  COMS1      
  CMP  #$DB       
  BGE  COMS1

The brackets mean things. The ",Y" means something. The #$C1 means a literal hex value. That's syntax. You learn it. You use it. Then you put logic on top of that. Same as in C.

You don't write stuff like:

   LDA Y,(SRCE),$42

That would give you a syntax error.

[quote author=Nick Gammon link=msg=2239921 date=1432121983] Can't agree with that. There is always syntax. And always logic.

For example, in the old 6502:

  LDA  (SRCE),Y   
  CMP  #$C1       
  BLT  COMS1      
  CMP  #$DB       
  BGE  COMS1

The brackets mean things. The ",Y" means something. The #$C1 means a literal hex value. That's syntax. You learn it. You use it. Then you put logic on top of that. Same as in C.

You don't write stuff like:

   LDA Y,(SRCE),$42

That would give you a syntax error. [/quote] True, I guess I didn't think of it as syntax!