0

What is the value of z when the following code finishes executing?

int x;
int y;
int z;

x = 1;
z = 1;

while (x <= 5)
{
  z = z + x;
  x = x + 1;
}

I know the answer is 16 but I keep running into errors when trying to compute it. I feel like I'm messing up a step in the loop in the beginning. I know I can just plug this into a java client and get the answer but I'm really trying to understand the loop sequence so as to be able to do it by hand.

3
  • 3
    Tutorial on while loops. Commented Feb 24, 2014 at 22:51
  • 1
    What errors are you running in to? Additionally, what about the loop / code is unclear? Commented Feb 24, 2014 at 22:53
  • 1
    I was just unsure as to how the loop was reaching the value of 16 for z as with each iteration I was just trying to add up but i was getting mixed up as far was what the subsequent z values would be. the below answers helped a lot ! Commented Feb 24, 2014 at 23:00

7 Answers 7

5

To really understand it, just think like the computer. Write out what the while-loop is going to do to each of the variables on each iteration. I've omitted y because it doesn't seem to be used for anything.

                x |  z | (x <= 5) | z + x | x + 1
               ===+====+==========+=======+======
Initial:        1 |  1 |          |       |     
               ===+====+==========+=======+======
Iteration 1:    1 |  1 | true     |     2 |     2
Iteration 2:    2 |  2 | true     |     4 |     3
Iteration 3:    3 |  4 | true     |     7 |     4
Iteration 4:    4 |  7 | true     |    11 |     5
Iteration 5:    5 | 11 | true     |    16 |     6
Iteration 6:    6 | 16 | false    |       |
               ===+====+==========+=======+======
Final values:   6 | 16 |          |       |

When x equals 6, the test x <= 5 evaluates to false, so we break out of the loop. And now, at the end, we can see that z = 16.

Sign up to request clarification or add additional context in comments.

Comments

4
    x = 1;
    z = 1;

Okay, x is 1 and z is 1.

    while (x <= 5)
    {
      z = z + x;
      x = x + 1;
    }

x is less than or equal to 5, so we perform this operation. We set z to 1+1 or 2. We set x to 1+1, or 2.

x = 2
z = 2

    while (x <= 5)
    {
      z = z + x;
      x = x + 1;
    }

2 is <= 5, so we loop. z = 2+2, or 4. x = 2+1, or 3.

x = 3
z = 4

    while (x <= 5)
    {
      z = z + x;
      x = x + 1;
    }

x is less than or equal to 5. Now z=4+3=7, x=3+1 = 4.

x = 4
z = 7

    while (x <= 5)
    {
      z = z + x;
      x = x + 1;
    }

Again, 4<=5, so z=7+4=11, x=4+1=5

x = 5
z = 11

    while (x <= 5)
    {
      z = z + x;
      x = x + 1;
    }

5 is <= 5, so we loop. z=11+6=16, x=5+1=6

x = 6
z = 16

    while (x <= 5)
    {
      z = z + x;
      x = x + 1;
    }

Oops, x is not <=5, so we're done. z is now 16.

Once you understand how this loop works, it's obvious. We start out at 1. Then we add 1, 2, 3, 4, and finally 5 to it. 1+1+2+3+4+5=16.

Comments

2
x=1
z=1
while(x is less than or equals to 5) {
z is z + x;
x is x+1
}

meaning first loop will be

z = 1 + 1 = 2
x = 1 + 1 = 2

second:

z = 2 + 2 = 4
x = 2 + 1 = 3

and so on until x is 5

Comments

2

When learning loops, making a chart can make it a bit easier to see what is going on (though it is a bit tedious)

z  | x | while (x <= 5) | z + x | x + 1 | new z | new x
=======================================================
1  | 1 | true           |   2   |   2   |   2   |  2        
_______________________________________________________
2  | 2 | true           |   4   |   3   |   4   |  3
_______________________________________________________
4  | 3 | true           |   7   |   4   |   7   |  4
_______________________________________________________
7  | 4 | true           |   11  |   5   |  11   |  5
_______________________________________________________
11 | 5 | true           |   16  |  6    |   16  |  6
_______________________________________________________
16 | 6 | false          |  NA   |  NA   |  NA   |  NA
^ final value

By writing a table you can see how the value changes over time. Once you reach false, you know that your final value for z is the output.

Also, you can start to understand how the for-loop works and notice patterns

Comments

1

The while statement keeps running untill a condition is true, in your case the condition is x<=5, but let's go step by step.

When the code first reachs the while your condition is true because x=1, so the block inside is executed and z = 1 + 1 = 2 and x = 1 + 1 = 2, then the iteration starts again, this time z = 2 + 2 = 4 and x = 2 + 1 =3 and so on until x > 5 that renders the condition false and the program exit the while loop. At that point z will be 16.

Comments

1

This while loop is looping while x is less or equals with 5 and in each iteration it increase values of variables z and x.

You can use debugger to view all values step by step, or you can modify your code and print all values from variable like this:

int x;
int y;
int z;
int iteration = 1;

x = 1;
z = 1;

while (x <= 5)
{
  z = z + x;
  x = x + 1;
  iteration++;
  System.out.println("Iteration: " + iteration + " Values: x=" + x + " y=" + y + " z=" + z);
}

2 Comments

Thank you for including the System.out.println statement!
You're welcome, it is quick change, but you will be more effective if you use debugger and see what is happening in each iteration. In this case you can view all statements, values, etc. I suggest you to try this.
0

You can rewrite it like this, to make it clearer:

x = 1;
z = 1;

while (x <= 5)
{
  z += x;
  x += 1;
}

Z is incremented by x at each loop, while x is incremented by 1.

In other words :

z = 1;

for (x = 1; x <= 5; x++)
{
  z += x;
}

I'm pretty confident is easier to trace by hand now, isn't?

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.