What Value Does X Have at the End of a Loop in Java?

What Value Does X Have at the End of a Loop in Java? Featured Image

Today, I’m kicking off a series of new articles that take a look at real computer science multiple choice questions and attempt to give some tips for solving them. Right now, I want to start by talking about loops and how to trace them. Let’s get into it!

Table of Contents

Multiple Choice Question

What value does `x` have at the end of the following loop?

int x = 0;
int i = 1;
while (i < 5) {
  x += i;

Select one of the following:

  • A. 15
  • B. 10
  • C. 1
  • D. 16


While you’re thinking about the answer (and so you don’t accidentally see the answer which is below), let’s talk briefly about how you can help The Renegade Coder grow.

The Renegade Coder, as it currently stands, is a solo operation. I’ve been at this since late 2016, and I’d love to make this style of content my full-time job. If you see value in the work I’ve put into these articles, you can show your support by subscribing to my PatreonOpens in a new tab.. Other ways to support the site including signing up for the mailing list and joining the DiscordOpens in a new tab..

In addition to writing, I dabble a bit in YouTube. Here’s my most popular video:

With that out of the way, let’s try to solve the problem.

Problem Solution

As with any multiple choice question, there are multiple ways to go about solving it. For this problem, I would actually recommend tracing the loop before trying any of the usual multiple choice tricks. There are plenty of ways to go about tracing, but the following is the approach I use with my students:

`int x = 0;`
x = 0
`int i = 1;`
x = 0
i = 1
`while (i < 5) {`
x = 0, 1, 3, 6
i = 1, 2, 3, 4
`x += i;`
x = 1, 3, 6, 10
i = 1, 2, 3, 4
x = 1, 3, 6, 10
i = 2, 3, 4, 5
x = 10
i = 5

If you’ve never seen a table like this (and perhaps I’ll write an article on how to make them), the idea is that we have each line of the program in every other row of the “statement” column of the table. Alternating rows with each statement is the “state” of the program. In other words, when a line of code is executed, we report the change in state in the other column.

Things get a bit more complicated when statements are executed multiple times—such as in loops. That said, the idea remains. Every time state changes, we update it. To be extra clear, we cross out old values.

If you follow through the entire trace, you’ll find that `x` is 10 when the code finishes executing.

Alternative Problem Solving Techniques

Let’s say for a second that you didn’t know how to trace this loop. Could you still solve this problem? I’d like to think you could.

My usual approach is to use process of elimination. In other words, which answers are unlikely? To do that, we’d need to think about everything that we know. For example, we know that `x` starts at 0 and `i` starts at 1. Unfortunately, this information alone doesn’t really help us eliminate any answers.

If we dig into the loop a bit, we can uncover some more information. For example, we know that `i` will eventually be 5 or greater. Otherwise, the loop won’t terminate. On top of that, we know that `i` increments in steps of 1 (i.e., `i++`), so we can actually predict the whole sequence of values for `i`: 1, 2, 3, 4, 5.

If we know the entire sequence for `i`, then we can begin to make some guesses about the final value of `x`. Specifically, we know that `x` depends on `i` from the statement `x += i`. If we think about it, `x` is really just the sum of the sequence of `i`. Therefore, any of the following answers could be possible values of `x`: 1, 3 (i.e., 1 + 2), 6 (i.e. 3 + 3), 10 (i.e., 6 + 4), or 15 (i.e., 10 + 5). Because 16 is not in this list, we can eliminate D (i.e., `x = 16`).

The final step is to then figure out which subset of the sequence of `i` makes up the sum of `x`. This requires a bit more reasoning than the previous work, but it can be done. If we look at the initial values of `x` and `i`, they’re 0 and 1, respectively. Because the summation line happens before `i` increments, we know that the first value of `i` we add is 1. Therefore, we just need to figure out the last term that is added. Again, because summation happens before the increment happens, we can conclude that the last value of `i` is never added. As a result, the final sequence is: 1, 2, 3, 4. When summed together, we get a value of 10.

There are probably other ways to reason about this code without actually tracing it, but I find tracing to be easiest. For loops with hundreds of iterations, a process like this may be more reasonable. That said, it’s still probably easier to trace a few terms and extrapolate the sequence than it is to try to reason about what the loop is doing. Regardless, I thought it would be fun to try approaching this problem from multiple perspectives.

Happy Learning

I figured I’d start this series off with a light looping problem. Perhaps as we go through the series, we’ll dig through more complex problems. That said, let’s call it a day.

As mentioned once already, if you liked this article and would like to see more like it, consider showing your support. Alternatively, you can check out some of these related articles:

Otherwise, take care! See you next time.

A Computer Science Problem a Day (3 Articles)—Series Navigation

As an educator, I tend to dislike examination as a form of summative assessment. However, I do like multiple choice questions as a form of formative assessment—especially in the context of peer instruction. If done correctly, multiple choice questions are great way of testing concepts and exposing misconceptions. As a result, I thought it would be fun to put together a little series covering questions I’ve seen or used as a way of teaching computer science concepts. Enjoy!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Code Posts