That completely depends on what you need the loop for, in any case, each will have it's advantages and disadvantages.
A while loop is good for cases where you don't exactly know when it is you'll want to break out of the loop. Say I'm reading the contents of a file, and I don't know how many lines there are, I would use a while loop that reads in line by line until the end of the file is reached. Once that happens, the condition in my while loop would fire and break me out.
For loops are good for initializing arrays:
I.e.
Code:
int main(void)
{
int MyArray[10000]; //It'll be a pain to set each value to 0
for (int x = 0; x < 10000; x++)
{
MyArray[x] = 0;
}
return 0; //Added this in after, otherwise it would be indented
}
Note though, that this can also be accomplished with a while loop:
Code:
int main(void)
{
int MyArray[10000]; //It'll be a pain to set each value to 0
int x = 0;
while(x < 10000)
{
MyArray[x] = 0;
x++;
}
return 0;
}
A for loop just puts it all together into a nice neat package. For are good for running through a loop when you know a certain amount of times you want to repeat code.
Any time you could use a for loop, you can use a while loop, and vice versa (I stand by this, unless there are any challenges?) It's just that at times, one is more efficient or logical to use, but it completely depends on the situation you need it for.
Note: These are just a few examples, initializing Arrays is not the only use for for loops, and reading files is not the only use for while loops. It's best to look at the situation you're in, and make a judgement call based on what you think you'll need.