Why are you decrementing then incrementing the loop counter in the FOR loop? You should not touch the loop counter...
so, I can leave the loop's body empty? whatsoever, thei = i + 1 - 1, right?
1- Why can't you use i in multiple FOR loops if i is defined in main() ?
2- What can't you do the same? What's preventing you from using i in the WHILE loop and FOR loop?
3- I'm unclear what you 'discover'? Unless I misunderstand you most of what you're saying is wrong.
ermm, I'm trying to claim the difference between 'for' and 'while' loop, which is 'for loop' defined the 'counter' in its own block.
but, the (while loop)'s 'counter' must be defined before it's own block itself...
so...
1- can be used, but for while loops, when we define the 'i' we need to define it again and again to reset it, maybe this code can give clue what I'm trying to say...
instead of this code (which will giving potential such variable would clash; if the program is bigger than these)
#include <iostream>
int main()
{
int counter = 0;
while (counter < 10)
// some stuff
++counter;
// counter = 10 here...
counter = 0;
while (counter < 10)
// some another stuff
++counter;
}
we can use these one...
#include <iostream>
int main()
{
for (int counter = 0; counter < 10; ++counter)
{
// some stuff;
}
// the 'counter' is not defined here
for (int counter = 0; counter < 10; ++counter)
{
// some stuff.
}
}
conclusion : I'm claiming that 'for loop' and 'while loop' has different functionality and specific for it own task, even though you can use any of it for the same task.
2- possible, look on the first code, but can become possible source of bugs for a certain program.
3- I hope it's clear now?! :)
P.S. similar to getline(cin, string) and while(cin >> string), you can create a code that has same function using either one of those, thought so, string willseperate each input by whitespace and getline() seperate each input by newline