Correct
In a for loop i++ and ++i are exactly the same. But here is an example to understand them better:
Code:
i = 5;
a = i++;
b = i;
VS
Code:
i = 5;
a = ++i;
b = i;
The equivalent codes are:
Code:
i = 5;
a = i;
i = i + 1;
b = i;
VS
Code:
i = 5;
i = i + 1;
a = ++i;
b = i;
So in both cases b = 6. But in the first case a = 5 and in the second a = 6.
You can also use while for a loop. That is simpler. You just loop until one condition is made:
Code:
while(flag == true)
{
...
}
Will loop until flag is not true. Thus false. How flag will become false depends. The equivalent for loop we posted below can be written with a while loop like this:
Code:
int i = 0;
while(i < 5)
{
std::cout << "Hey, dude" << std::endl;
++i;
}
A for loop is kind of more "powerful" than a while loop, but both can do exactly the same things.
Here is another common example with a while loop. This will print all the elements inside an array of size arrSize:
Code:
int i = 0;
while (i < arrSize)
std::cout << array[i++];