jeudi 27 octobre 2016

How to solve for x when observing through a for loop

Assume x is an integer and has been initialized to some value. Consider the code

for (int a = 1; a < 20; a++)
    if (x < 0)
        x = a;

Which statement will have the same effect on the value of x?

a.if (x < 0) x = 1; b.if (x < 20) x = 19; c.if (x < 0) x = 19; d.if (x < 20) x = 20; e.x = 1;

For this problem I was a little confused on what the question was asking because the answer was not what I was expecting. I chose answer choice b.if (x < 20) x = 19; because 19 is less than 20 and the other answer choices are false when plugging into 'x'. The answer to this question is actually a.if (x < 0) x = 1;. I am confused on why answer choice a. is the answer.

Aucun commentaire:

Enregistrer un commentaire