In terms of the equivalences between for loops and while loops, it's basically this:
for (INIT; CONDITION; POSTOP) { INIT;
BODY; while (CONDITION) {
} BODY;
POSTOP;
}
(with variations for scope and other such things that we don't need to go into here).
Hence, to solve your problem with a for/while solution, you could use something like:
for (int i = 0; i < 5; i++) {
int j = i;
while (j < 5) {
System.out.print("*");
j++;
}
System.out.println();
}
It's sometimes helpful to run through the code in your head, with a pen and a bit of paper to maintain variables, such as:
i j output
--- --- ------
If you just "execute" each line of the code (either your original or my for/while variant) in your head for a few iterations, you should see what's happening. And, if you do them side-by-side, you'll see the equivalency between both variants.
Basically, the outer loop counts (iterates) from 0 to 4 inclusive, running the inner loop then outputting a newline character.
For each of those iterations, the inner loop counts from i to 4 inclusive, outputting a * each time (with no newline character).
So, in the first outer loop iteration, the inner loop runs from 0 to 4, outputting five stars.
In the second outer loop iteration, the inner loop runs from 1 to 4, outputting four stars.
And so on, to the final outer loop iteration where i is 4, so the inner loop runs from 4 to 4, outputting one star.
In terms of the pen-and-paper method, you would get something along the following lines:
i j output
--- --- ------
0 0 *
0 1 *
0 2 *
0 3 *
0 4 *
\n
1 1 *
1 2 *
1 3 *
1 4 *
\n
2 2 *
2 3 *
2 4 *
\n
3 3 *
3 4 *
\n
4 4 *
\n
whileloop. Please add one.