1

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.

4
  • Are you sure the accolades are correctly written? These have an important impact... If possible, can you make a scan of the assignment? May 1, 2016 at 21:56
  • 3
    20 is definitely the correct answer for the given program, but perhaps you forgot a few details. May 1, 2016 at 21:59
  • z wouldn't amount to 25 or 29. It would amount to 20. If you moved your System.out.println out outside the loops, it would only print the last value: 20. You are executing a loop 5 times, which executes a loop 5 times. 5x5 = 25. subtracting one for every 5 you add, 5x5 - 5 = 20. May 1, 2016 at 21:59
  • @WillemVanOnsem you are right there is a typo. Sorry!! I'm adding the correct code now
    – PKM357
    May 1, 2016 at 22:19

6 Answers 6

1

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;
1
  • Thanks, Willem, I really really appreciate the detailed answer here! If it comes up again on the final, I will be ready for it :)
    – PKM357
    May 1, 2016 at 22:45
0
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

0

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

0

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

0
0

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

2
  • sorry, I reedited the code, my mistake, can you reevaluate if it's actually 29?
    – PKM357
    May 1, 2016 at 22:24
  • @PKM357 There's the revised answer
    – hjl
    May 1, 2016 at 22:32
0

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.

4
  • I did have a typo. I apologize for that!
    – PKM357
    May 1, 2016 at 22:37
  • @PKM357 I edited my answer. hope it helps. Going for that Thumbs up^)^
    – DarkV1
    May 1, 2016 at 22:40
  • Thank you very much! I tried to upvote you but I need 15 reputation apparently to publicly change/upvote.
    – PKM357
    May 1, 2016 at 22:42
  • Its fine. No Problem. glad to help ! ^_^
    – DarkV1
    May 1, 2016 at 22:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.