
Title:
While Loops Solution  Intro to Computer Science

Description:

So the answer is: it prints out the numbers, from 1 to 10.

And if we follow through the code, we can see why.

So initially, the value of "i" is zero.

The test says, well "i" is not equal to 10,

so zero is not equal to 10

so we go through the Block,

and the Block adds 1 to "i".

So that will change the value of "i".

Now "i" refers to "1".

And then the next statement prints the value of "i".

The value of "i" now is 1, so it will print, "1".

And then, because it's a while, we go back.

We do the test againI'm not going to step through every time, so we keep doing this.

As "i" gets bigger, we're going to keep going.

Eventuallylet's say "i" is 9,

and by the time "i" is 9well, after "i" was 9we print "9".

So we've printed the numbers from 1 through 9.

At this point, "i" is still not equal to 10,

so we still go through the loop body.

Now we add 1 to "i"that will make the value of "i" 10.

Then we do the print, so that will print out, "10".

Then we go back to the "while" test.

Now the value of "i" is 10, so: "i" not equal to 10"10" not equal to 10

is False, because 10 does equal 10.

That means we're done with the "while" loop

and we're going to continue,

but there's nothing to continue with, so we're done.

So the net result of the code is printing the numbers from 1 to 10

and that's all that happens.

So now the test is False.

We don't execute the Block anymore, we would continue with the next statement.

There isn't one, so we're done

and what the code did was print out the numbers from 1 to 10.