A while-loop loops as long as it's predicate (the expression in the braces following the while-keyword) is
true
:
bool keepRunning = true;
while(keepRunning)
{
if(! UserWantsToKeepRunning())
keepRunning = false;
}
A for-loop usually (as a beginner you shouldn't care about non-standard uses yet) increments or decrements a variable with a given starting-value on each loop until the variable reaches a value so that the predicate of the for-loop is no longer
true
:
for(int x = 0; x < 10; x++)
{
}
You usually use a for-loop if you need a variable running from a start value to an end value and a while-loop if you don't know when writing the code how many loop-runs will be required when your program is executing.
It's easy to "emulate" a for-loop with a while-loop (just an academic example, no real use):
int x = 0;
while(x < 10)
{
x++;
}