DEV Community

Discussion on: The difference between x++ and ++x

Collapse
 
riyasrawther profile image
Riyas Rawther

one more Example:

Please visit dartpad.dev/. The default sample code when you open the dart-pad is

void main() {
  for (int i = 0; i < 5; i++) {
    print('hello ${i + 1}');
  }
}

Run the sample code.

Note the line # 3 - print('hello ${i + 1}'); The i + 1 is used because initial value is 0. Now think how to avoid that with our prefix increment ++x.

void main() {
  for (int i = 0; i < 5;) {
    print('hello ${++i}');
  }
}

The output will be same.