Go Down

Topic: String += Help (Read 1 time) previous topic - next topic


Been trying to figure out what this line does in the code below.  I've done a log of reverse engineering and found that if I set the (val) to (1) I get the high response or whatever I put in that first "" same thing goes if I put (0) in the place of Val I get Low or whatever I change the word to... But just don't understand how its working because I can't do s += (val)?"high":"Low":"LowLow";

 s += (val)?"high":"Low";     

so really just want to know what the ? does. I know this is probably really basic but looked on the leaning center under strings and can't figure it out.

thanks let me know if you need more info.

int val;                                               // create the Integer val
  if (req.indexOf("/gpio/0") != -1)              //if the data sent to the webserver "request" matches /gpio/0"
    val = 0;                                           // Set val to 0             
    Serial.println(val);                              // print the val to serial
  else if (req.indexOf("/gpio/1") != -1)         // if the data sent to the webserver "request" matches /gpio/1"
    val = 1;                                         // Set val to 0
    Serial.println(val);                           // print the val to serial
  else {                                               // for everything else print invalid request
    Serial.println("invalid request"); 

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";      // http response
  s += (val)?"high":"Low";                                                                                         
  s += "</html>\n";

  // Send the response to the client
  Serial.println("Client disonnected");



Code: [Select]
s += (val)?"high":"Low";
If val is true append "high" to the String s else append "Low" to it

The C ternary operator in all its glory.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


The ternary operator is shorthand for an if-else block. In your case:

Code: [Select]

   if (val != 0) {
      s += "high";      // s = s + "high"
   } else {
      s += "Low";       // s = s + "Low"

That is, if the logical test on val is logic True (i.e., non-zero), "high" is added to s. If val is 0, then "Low" is added to s. Evidently, s is a String variable.


It is indeed a ternary operator:
Code: [Select]
<condition> ? <value if true> : <value if false>;
A simple example would be:
Code: [Select]
int valueA = 12;
int valueB = 7;
Serial.println((valueA > 10) ? "Greater than 10" : "Less than 10");
Serial.println((valueB > 10) ? "Greater than 10" : "Less than 10");

This will print:
Code: [Select]
Greater than 10
Less than 10

P.S: Please use code tags, using the </> button or by using [code][/code] around your code.


Syntax details...
Notice that a single question mark separates the boolean test from the values.
Notice that a SINGLE colon is used to separate the two (and only TWO) values.


It adds either one to the string. Then keeps adding stuff to the string before sending it to the browser to show one of two messages.


Wow first off thank you all that responded. I didn't have alerts on this post and didn't think anyone was responding.

I'm a network engineer and I have super limited experience with any programming so all the help is much appreciated.

Fully understand how it works now really motivated to keep learning now that I know there are many people to help when needed.

Thanks again.

Go Up