PKM357
PKM357

Reputation: 13

Nested loop clarification - Edited

I'm an intro Java student, and I'm studying for a final exam. I'm going over a prior quiz on repetition statement loops, and see an answer I got wrong and cannot figure out for the life of me why the answer is the way it is.

Can someone please tell me why z = 29 in the following case (and not 25)? My Professor marked it wrong saying it was 29 but without explanation.

int z = 0;
for(int x=0; x<5;x++){
    for(int y=0; y<5;y++){
        z++;
    }
    z++;
}

z--;

The only way I was able to get 29 was by the inner loop executing 5 times (same for outer loop therefore, 5*5=25 plus 4?? Also I've executed this in my IDE and the console printed this when I tried system.out.println(z);

4
8
12
16
20

(so I'm now fairly confused) Any assistance you could provide would be greatly appreciated.

Upvotes: 1

Views: 78

Answers (6)

DarkV1
DarkV1

Reputation: 1776

It runs through the inner loop 5 times for every increment in x. And for every increment of x.. adding up to a total of 5 times... it substracts one...

Therefore,

5*(5 + 1 )-1 = 29.

The answer is 20 not 29. Unless you have a typo.

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

Your code fragment:

int z = 0;
for(int x=0; x<5;x++){
    for(int y=0; y<5;y++){
        z++;
    }
    z++;
}
z--;

Indeed results in z being equal to 29. Lets calculate the difference for z for each part. The inner for loop iterates five times for each iteration of the outer loop so:

for(int y=0; y<5;y++){
    z++;
}

is basically equal to:

z += 5;

We thus rewrite the program to:

int z = 0;
for(int x=0; x<5;x++){
    z += 5;
    z++;
}
z--;

Now z += 5; and z++ is equal to z += 6;. We rewrite the program to:

int z = 0;
for(int x=0; x<5;x++){
    z += 6;
}
z--;

The for loop is again repeated five times so the update for z is: z += 5*6; or z += 30;. We rewrite the program to:

int z = 0;
z += 30;
z--;

Finally we merge the three statements into one: int z = 0+30-1; or

int z = 29;

Upvotes: 1

hjl
hjl

Reputation: 15

With each iteration of the inner for loop z increases by 1. With each iteration of the outer for loop z decreases by 1.

Each time the inner loop loops five times, z is incremented by five.

Each time the outer loop completes one loop, z is decremented by one and the inner loop loops five times, therefore, z increments by 4 with each iteration of the outer loop.

The outer loop loops five times => (z+=4)*5 => z=20

Edited

With each iteration of the inner for loop z increases by 1. With each iteration of the outer for loop (not including the inner loop) z also increases by 1.

Each time the inner loop loops five times, z is incremented by five.

Each time the outer loop completes one loop, the inner loop loops five times and z is incremented by one, therefore, z increments by 6 with each iteration of the outer loop.

The outer loop loops five times => (z+=6)*5 => z=30

And finally, you subtracted one at the very end, independently of the loops => z = 30 - 1 = 29

Upvotes: 0

basda
basda

Reputation: 1

The correct answer is 20. Perhaps his or her 20 looked like 29 while marking.

Upvotes: 0

Striker
Striker

Reputation: 505

Are you sure is 29 and not 20? Professors can have misleading writing styles at times.

The answer to the question should be 20

int z = 0;
for(int x=0; x<5;x++) //0 1 2 3 4  this loop will run 5 times
{ 
    for(int y=0; y<5;y++)//0 1 2 3 4  this loop will run 5 times
    { 
        z++; 
    }
    z--;
}

Answer is: z = outer loop * (inner loop - decrement) z = 5 * (5 - 1) z = 20

Upvotes: 0

Troncador
Troncador

Reputation: 3536

int z = 0;
// iterate 5 times
for(int x=0; x<5;x++){
    // iterate 5 times
    for(int y=0; y<5;y++){
        // z=z+1  
        z++;
    }
    // z=z-1
    z--;
}

Mathematically z=5*(5*(+1)-1)

Then z=20

Upvotes: 0

Related Questions