Even though it is possible to convert any of the 3 looping statements to another, there are times where one particular form is preferred.
In this case, I think one is better off staying with the while loops.
For loops are good when the number of times to loop is known, while loops are better when the end condition depends on something else.
The last example
1 2 3 4 5
|
while (num == num2) { // equality comparison, not assignment
cout << num << endl; //send value of num
break; //breaks
}
|
is equivalent to an if statement. :
1 2 3
|
if (num == num2) {
std::cout << num << std::endl;
}
|
And the form
while(num == num2) {}
could easily lead to an infinite loop, if one forgot to break out of it. So for this reason, I would avoid it.
Here is some more info, just in case you weren't aware already :
Infinite loops are needed when the end condition is complicated and / or dependent on other variables. They must have a coherent base case, that is, a sane exit point.
I am old fashioned, so I always use
for(;;)
rather than
while(true)
for infinite loops, because it will always work in any C like language.
IMO, beginners should not routinely use infinite loops because they are too lazy to think of an end condition. Obviously they are fraught with danger when no end condition is provided at all, anywhere in the loop.
Hope all goes well :-)