How to read a loop that does not have braces?

advertisements

I'm trying to read the output of this code, but it simply just doesn't make sense to me.

Even after learning that a loop without braces only loops through the first line, the output still makes no sense, at all. Some numbers do, but others just don't.

My code:

int n = 8;
int i = 1;
int j = 1;

j = n + 2;

System.out.println (n + " " + i + " " + j );

while (n > 1)
{
    n = n/2;
    for (i = 2; i <= n; i = i+2)
        System.out.println (n + " " + i + " " + j );

    j++;
}
System.out.println (n + " " + i + " " + j );

And it outputs:

8 1 10
4 2 10
4 4 10
2 2 11
1 2 13

I get the 8-1-10 and the 4-2-10

but anything after that, I'm stumped, I don't get how the computer calculates the rest.

Would someone mind going through the rest of the output with me, step by step?

Thank's in advance.


No braces means that the loop affects only the next statement that follows the loop.

So

for (i = 2; i <= n; i = i+2)
System.out.println (n + " " + i + " " + j );

is equivalent to

for (i = 2; i <= n; i = i+2)
{
    System.out.println (n + " " + i + " " + j );
}

Usually, indentation is used in those cases to make the code more comprehensible, like this:

for (i = 2; i <= n; i = i+2)
    System.out.println (n + " " + i + " " + j );

EDIT: Now, this is the actual answer to the question. It all depends on the different iterations the loop does and how do the variables get incremented.

int n = 8;
int i = 1;
int j = 1;

j = n + 2; //This means that j takes the value 10.

System.out.println (n + " " + i + " " + j ); // 8 1 10 So far, so good.

Now, on with the iteration:

while (n > 1)
{
    n = n/2;
    for (i = 2; i <= n; i = i+2)
        System.out.println (n + " " + i + " " + j );

    j++;
}

For the first iteration, we have n=8 i=1 j=10, so since n > 0 is true the loop takes place.

n = n / 2; //n = 4

Then, the for (note that it just assigns the value 2 to i):

for (i = 2; i <= 4; i = i+2) //Since n = 4

Since n = 4, the only values that i can take are 2 and 4, then the prints are:

4 2 10
4 4 10

After that, j is incremented by one, making it j = 11. The condition for the while is met again because n = 4. n = n/2 makes n take the value 2, so it enters the while again. Let's take a look at the for again:

for (i = 2; i <= 2; i = i+2) //Since n = 2

This time, the only value that i can take is 2 (note that the value of i is reset again to 2 while starting the iteration), and that's the print you get.

2 2 11

Before iterating again, j++ makes j have the value 12.

On the final iteration, n = n/2 results in n = 1 since n is an int but this is done inside the while, so it enters again. Now that n = 1 the loop looks like this:

for (i = 2; i <= 1; i = i+2) //Since n = 1

i is set to 2 and the condition for the for is not met (2 <= 1 is false). Then there is no print this time, yet j is incremented to 13 at the end of the while.

In the next iteration you have n = 1, that means that the while's condition is not met so the iteration breaks. Finally you have n = 1, i = 2 and j = 13. That's the last print you get.