I am having troubles with nested while loops in a for loop. I understand the nested for loop:
for (int i = 0; i<5;i++)
{
for (int j=i;j<5;j++)
{
System.out.print("*");
}
System.out.println();
}
When it comes to a while loop inside a for loop I'm kinda lost, can someone explain it to me?
Expected output:
*****
****
***
**
*
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
Well, first your for
loop(s) could be written like
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
Next, let's look at the three parts of a for
loop (from the Wikipedia link)
for(INITIALIZATION; CONDITION; INCREMENT/DECREMENT){
// Code for the for loop's body
// goes here.
}
We can move that to a while
loop like
INITIALIZATION;
while (CONDITION) {
// Code for the while loop's body
// goes here.
INCREMENT/DECREMENT;
}
As a single practical example,
int j = i;
while (j < 5) {
System.out.print("*");
j++;
}
A while loop inside a for loop can be made to behave like a nested for loop if you simply update the counter variable within the body of the while loop. Alternatively, think about what a for loop is really saying per iteration.
for (int i = 0; i<5;i++)
{
int j = i;
// for (int j=i;j<5;j++)
while(j < 5)
{
System.out.print("*");
j++;
}
System.out.println();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.