160k views
3 votes
2. What will be the value of x?

var x = 0;
for(var i = 0; i < 4; i++) {
x = x + i;
}



Python

User CJBrew
by
5.1k points

1 Answer

4 votes

Answer:

The value of x after the loop completes will be 6.

Step-by-step explanation:

Here's how the loop works:

The loop starts with i equal to 0. x is currently 0, so x becomes 0 + 0, or 0.

The loop continues and i becomes 1. x is currently 0, so x becomes 0 + 1, or 1.

The loop continues and i becomes 2. x is currently 1, so x becomes 1 + 2, or 3.

The loop continues and i becomes 3. x is currently 3, so x becomes 3 + 3, or 6.

The loop ends because i is now equal to 4, which is not less than 4.

So, the final value of x will be 6.

User DeadEli
by
5.2k points