OK, I think this problem is more close to philosophy. :P
I've some idea now. But I'm not sure whether it's correct. For the purpose of convenience, I will use some C-like code instead of Mathematica's.
A While
loop is something like:
i=0; // some initialize of variable i
While(test of variable i) // such like i<10 or i^3<90, etc.
{
// involving doing something which has nothing to do with i (e.g. Print["*"])
// or involving taking i as an argument of another function, such like Sin[i]
// or involving re-assign some value to i, such like i=i+7
}
Print[i] // after doing such evaluations, we want to get the final value of i
The first and second line surrounded in while is not really a problem for NestWhile
.
It is just because NestWhile
is NestWhile[f, expr, test]
,
one can write (Print["*"]; Sin[#] + 1 &)
in the place of f
, doing the same thing you want when you're dealing with While
.
However, when you want to re-assign expr
in NestWhile
, No, you cannot.
i = 0; NestWhile[# = # + 1 &, i, # < 70 &]
is not what you want. (The i
is immediately change into 0 before you would want to do i = i+1
)
And just because of this simple difference between While
and NestWhile
,
I doubt that EVERY While
-code can change into NestWhile
-code.