1

I'm working with an esp8266 and want to have it default to an access point when it cannot connect to a wifi network. I'm using chriscook8's example from github here.

chriscook8 uses "+=" in several places and I can't find an explanation of what that means.

Here is an example from setup():

void setup() {
  Serial.begin(115200);
  EEPROM.begin(512);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.println("Startup");
  // read eeprom for ssid and pass
  Serial.println("Reading EEPROM ssid");
  String esid;
  for (int i = 0; i < 32; ++i)
  {
    esid += char(EEPROM.read(i));  //<- here
  }

What does += do?

  • 5
    This belongs on stackoverflow.com, not here. Stackoverflow is the site for programming questions, as this asks about C++'s syntax. – noɥʇʎԀʎzɐɹƆ Jun 26 '16 at 20:54
  • Google for C++ operators – phuclv Jun 27 '16 at 7:43
8

It adds the value or variable on the right to the variable on the left and assigns the result to the variable on the left.

A += B

is the same as:

A = A + B

It's short-hand.

In the case of a String it concatenates the right hand item on to the end of the left hand item.

|improve this answer|||||
  • Thank you. Is it considered better form to use +=? – acpilot Jun 26 '16 at 16:57
  • It makes no real difference. It's just quicker to type. Also it's a historical thing - when storage space was at a premium saving bytes here and there could make a difference. I recall at University I had a maximum storage space of 4MB for all my work on the servers. Saving space like that was good. – Majenko Jun 26 '16 at 16:58
  • 1
    I'm not sure it's correct to say that it "assigns the result to the value on the left. A "value" is the result of evaluation; you cannot assign anything to a value. The right word would be variable, or maybe location. – Dave Cousineau Jun 26 '16 at 22:06
  • The computer will not function differently with A += 1 or A = A + 1, but the code is simply easier to read. If your variables are more descriptive, say AmountOfDaysWorked, then AmountOfDaysWorked += 1 is easier to read than AmountOfDaysWorked = AmountOfDaysWorked + 1. It gets worse if you have to add a variable to another variable. – Nelson Jun 27 '16 at 0:45
  • @Sahuagin: You're right that value is not precisely correct. But neither is variable: I can assign to str->field, but this is not a variable; it is a (compound) expression. The term you are looking for is lvalue ("ell-value"), which is merely "something that can appear on the left-hand side of an assignment expression." (Circular? Perhaps. But that is the term.) – wchargin Jun 27 '16 at 5:36
3

+= is a common programming shortcut. An example: (pseudo-code)

num1 = 7
num2 = 15
#these two lines are exactly the same
num1 += num2
num1 = num1 + num2

+= is extremely useful for increasing the value of a variable by another variable. It also works similarly with -=, *=, and /=

|improve this answer|||||
  • The assignment versions of binary operators include the arithmetic, bitwise logical, and shift operators: *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, and |=. Expr1 <opr>= Expr2 is equivalent to Expr1 = Expr1 <opr> Expr2, except that Expr1 will only be evaluated once (important if Expr1 has side effects). – JRobert Jun 26 '16 at 18:51
0

In the earliest days of the C language (I'm talking late 60's here), the compilers did not do much (if any) optimization, and a lot of the constructs in C were put in so that a programmer could get a more optimized program by using hints like +=. So, in those days, A = A + 5 generated three PDP11 instructions, while A += 5 was only a single PDP11 instruction. That meant that most programmers learned to write it that way. But because it was more concise and to many also more readable, the C constructs with <op>= have made it into most modern languages.

|improve this answer|||||
  • 1
    But as other comments have said, general programming questions have their own StackExchange sites...actually several of them... – MAP Jun 27 '16 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.