DEV Community

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

Collapse
 
riyasrawther profile image
Riyas Rawther
//DART
void main() {
int x;

//Postfix (e.g a++)
x =1; 
print (x++); // Prints the previous value. Output is 1
print (x++); // Previous value is 1 and adds 1 to it. Output is 2
print (x++); // Previous value is 2 and adds 1 to it. Output is 3
//note: The first value will be the same. Then incements with 1 each time when it called.
//PostFix
// example

for (int i = 0; i < 5; i++){
print('The values are: ${i}'); 
// note that the first number will be 0
// to start from 1 change the ${i} to ${i+1}
}

}