I know a while loop can do anything a for loop can, but can a for loop do anything a while loop can?
Please provide an example.
While loop
does not have as much flexibility as a for loop
has and for loops are more readable than while loops. I would demonstrate my point with an example. A for loop can have form as:
for(int i = 0, j = 0; i <= 10; i++, j++){
// Perform your operations here.
}
A while loop cannot be used like the above for loop and today most modern languages allow a for each loop
as well.
In my opinion, I could be biased please forgive for that, one should not use while loop
as long as it is possible to write the same code with for loop
.