Home > other >  Why is quantity ; not the same as print(quantity );? Is this a gotcha?
Why is quantity ; not the same as print(quantity );? Is this a gotcha?

Time:11-04

Example code:

void main() {
  int quantity = 300;
  print(quantity  ); // 300
}

I would have thought quantity would now equal 301?

void main() {
  int quantity = 300;
  quantity  ;
  print(quantity); // 301
  print(quantity  ); // 301 >> In this case    does nothing??
}

Seems to work fine though. Why does it not work as part of a print statement? For example print(quantity 1); works fine, so why doesn't print(quantity );?

What is happening under the hood?

CodePudding user response:

quantity means use the value of quantity, then increment it. If you did print(quantity ) print(quantity) you'd see the value has changed. Use quantity if you want the value incremented before you use it

  • Related