+ 2
it's optional or so is there a difference between two for loop with and post and pre-incrementation.....??
it's optional or so is there a difference between these two for loop: --1-- for (int i=0; i<10; i++){} --2-- for (int i=0; i<10; ++i){} I've tried both separately in the same program and the result is the same off I've never seen a pre-increment in a for loop .....
2 Respostas
+ 1
pre and post increment differs when yo assign them to some variable.
for example
for(){
a=i++;
}
for(){
a=++i;
}
this makes difference in both the loops
+ 3
There is no difference between them. If you break those down, it will be like this
first loop
for (int i = 0; i < 10;) {
...
i++;
}
second loop
for (int i = 0; i < 10;) {
...
++i;
}
Therefore, they are the same. I don't exactly know why most people use i++ instead of ++i, including myself. If I have to say, I would say that it's easier for me to read it