these code, I expect they work the same.
using Code::Blocks as the compiler, the result is differ
result = output 10
return 0
#include <iostream>
int main()
{
int i = 0;
while(i < 10)
++i;
std::cout << i << std::endl;
}
result = compile error.
#include <iostream>
int main()
{
for (int i = 0; i < 10; ++i)
{
// random stuff that not bother the result
--i;
++i;
}
std::cout << i << std::endl;
}
in the 2nd code, the int i is not defined in the main(), so, because of that, we can use multiple 'for loops' with same iterator(int i)?
unlike 'while loops', the int i declared in the main(), so we can't do the same as the 'for loops'?
well, I posts this because when I search internet for the difference between for and while, many people say it work the same and tell that the only difference is the form.
can anyone confirm of what I "discover"? :)