0

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:
*****
****
***
**
*
3
  • 2
    The code you've provided doesn't even contain a nested while loop. Please add one. Commented Mar 3, 2015 at 4:25
  • Time to do some desk checking. Get out a pen and piece of paper and walk the code, updating the values as you go... Commented Mar 3, 2015 at 4:26
  • 1
    or better than pen and paper, breakpoints!!! Commented Mar 3, 2015 at 4:31

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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++;
}

Comments

1

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.