Well if you wanted to do
i + 1
, that would have no effect, as it would add 1 to
i
and than do nothing with it, it isn`t going to do anything, You would rather do
i += 1
This is the same as
i + 1 = i;
So for sure you can do
for (int i = 0; i < 10; i+=1)
But as if for i + 0.01, same thing applies as above that it does not really do anything so you would need to write it as
i += 0.01
But now we have a different error, i is an int and it does not have a decimal point so what
0.01
will be turned into will be
, Which means the loop will never end. You will need to write a floating point number such as a double if you would like to do
i += 0.01
.
1 2
|
for (double d = 0; d < 10; d += 0.01)
std::cout << d << std::endl;
|
And just a few more facts when you did your
for (int i = 0; i < size; i++)
Well there is no error in this, well the i++
For example
1 2 3
|
int i = 1;
std::cout << i++ << std::endl; // This will print 1, not 2
std::cout << i << std::endl; // This will print 2
|
Now what this does is, that it prints the current value of i and then increments it, in you loop you could have written
++i
as we are not doing anything with the value before incrementing, so now look at this
1 2 3
|
int i = 1;
std::cout << ++i << std::endl; // This will print 2, it will increment first then print
std::cout << i << std::endl; // This will print 2 also
|
Here is more about the prefix and postifx ++ operator: http://www.hotscripts.com/forums/c-c/57931-c-tip-what-difference-between-postfix-prefix-operators.html