1.
What is the output of the code below?
int main() {
int x = 0;
int i = 1;
while (i < 10) {
x = i;
i++;
}
cout << x;
}
- 0
x
is initialized to 0, but it’s value is reassigned in the while loop. Can you figure out what the final value assigned tox
is?- 1
- When
i
is 1,x
is assigned the value ofi
, sox
is 1. However, the while loop continuously increments i, so the final value ofx
is not 1. - 9
x
is assigned the value of 9 during the last iteration of the while loop, and thus 9 is the output of the program.- 10
i
is incremented to a value of 10, but sincei < 10
is false, the contents of the while loop is not executed, sox
is never assigned the value of 10.