Heartbound07
Heartbound07

Reputation: 219

How do I read a loop that has no braces?

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.

Upvotes: 2

Views: 216

Answers (2)

Fritz
Fritz

Reputation: 10055

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.

Upvotes: 9

voluminat0
voluminat0

Reputation: 906

You start the while loop with n = 8, i = 1 and j = 10.

start while: (n=8 > 1) is true
n = 4
i = 2 (i <= 4, so do for loop, then change i to i+2=4)
print 4,2,10
i = 4 (i <= 4, so do for loop again, then i = i+2 = 6
print 4,4,10
i = 6 (i <= 4 is false, so exit for)
j++ so j becomes 11

next while:
n = n/2 = 2
i = 2 (i <= 2, so do for loop, then change i to i+2=4)
print 2,2,11
i = 4 (i <= 2 is false, so exit for)
j++ -> j = 12

next while:
n = n/2 = 1
i = 2 (i <= 1 is false, so exit loop)
j++ -> j = 13

exit while.
Final print= n = 1, i = 2 (value assigned in last for loop, loop doesn't get executed so i doesnt get incremented by 2), and j = 13

Hope this helps :)

Upvotes: 2

Related Questions