Having trouble understanding this section of code for serial monitor

I'm using a sunfounder tutorial to learn a bit about using Arduino, and I don't understand one of their tutorial codes for using the serial monitor. Here is the entire code:

//Serial Monitor
// open the serial monitor ,if you input red, you will see the red LED light up
//Email:support@sunfounder.com
//Website:www.sunfounder.com
//2015.5.7
const int greenPin= 2; //the green led pin attach to pin 2
const int yellowPin= 3; //the yellow led pin attach to pin 3
const int redPin= 4; //the red led pin attach to pin 4
String comdata = "";
int lastLength = 0;

void setup()
{
  pinMode(greenPin,OUTPUT); //initialize the greenPin as output
  pinMode(yellowPin, OUTPUT); //initialize the yellowPin as output
  pinMode(redPin, OUTPUT); //initialize the redPin as output
  Serial.begin(9600); // start serial port at 9600 bps:
  Serial.print("Please input any color of LED:\nOnly red, yellow, and green will have any function \n"); //print message on serial monitor
}

void loop()
{
//read string from serial monitor
if(Serial.available()>0) // if we get a valid byte, read analog ins:
{ 
  comdata = "";
  while (Serial.available() > 0) 
  { 
    comdata += char(Serial.read());
    delay(2);
  }
  Serial.println(comdata);
}

if(comdata == "red")
{
  digitalWrite(redPin, HIGH);//turn the red led on
  digitalWrite(greenPin, LOW);//turn the green led off
  digitalWrite(yellowPin, LOW);//turn the yellow led off
  digitalWrite(redPin,LOW);
}

else if(comdata == "yellow")
{
  digitalWrite(redPin, LOW);//turn the red led off
  digitalWrite(greenPin, LOW);//turn the green led off
  digitalWrite(yellowPin, HIGH);//turn the yellow led on
}

else if(comdata == "green")
{
  digitalWrite(redPin, LOW);//turn the red led off
  digitalWrite(greenPin, HIGH);//turn the green led on
  digitalWrite(yellowPin, LOW);//turn the yellow led off
}

else
{
  digitalWrite(redPin, LOW);//turn the red led off
  digitalWrite(greenPin, LOW);//turn the green led off
  digitalWrite(yellowPin, LOW);//turn the yellow led off
} 
}

The part I am having trouble with is this:

void loop()
{
//read string from serial monitor
if(Serial.available()>0) // if we get a valid byte, read analog ins:
{ 
  comdata = "";
  while (Serial.available() > 0) 
  { 
    comdata += char(Serial.read());
    delay(2);
  }
  Serial.println(comdata);
}

I don't understand how it is reading my input. comdata is a string, but is defined as being empty. Is it always like that, or does that change if I input a value into the serial monitor to then store that value? What's the comdata+= line doing? and why the delay?

Thanks!

The combination assignment operators in C are just a shorthand way of writing. x+=y is a simpler way of saying x = x + y

The + operator is defined for big-S strings as concatenation. Two strings are joined, one after the other. So comdata+=anything will add the contents of anything to the end of comdata. If comdata is initially empty, then the result will just be the contents of anything.

Big-S strings are useful for making certain operations like concatenation easy to write. However they cause other problems. There are very few (less than 1%) of serious Arduino programs that use Strings. So unfortunately, once you've learned this tutorial, you need to forget it.

However all of the concepts used in big-S Strings are used in full-size PCs and servers, so it's not a total waste.

Interesting. I didn't even notice the difference between big-S Strings and strings as character arrays. And the 2ms delay is just to compensate for the 9600 baud?

Thanks

Yes, 2ms is just bigger than the time it takes to transmit one char at 9600. It's a pretty crude way of doing it but it does work. Most real Arduino programs (like 50%) can't afford to wait 2 milliseconds for serial data to arrive. The Arduino can execute 32,000 instructions in that time. So go and do thirty thousand other things and then see if there's more serial data.

Time to read Serial Input Basics - updated ;)