Computer Science, asked by LoneWolf1405, 4 months ago

State the number of iterations for the following loop:
int a =0, x=0;
while (x + + <9)
a+=x;

Answers

Answered by MysterySD
5

Answer:

The while loop will rotate 9 times and the output of

a = 45.

Explanation:

Dry Run

(1)When a = 0, x = 0,

x++

=> x = x + 1

=> x = 0 + 1 = 1

a+=x

=> a = a + x

=> a = 0 + 1 = 1

(2)When a = 1, x = 1,

x++

=> x = x + 1

=> x = 1 + 1 = 2

a+=x

=> a = a + x

=> a = 1 + 2 = 3

(3)When a = 3, x = 2,

x++

=> x = x + 1

=> x = 2 + 1 = 3

a+=x

=> a = a + x

=> a = 3 + 3 = 6

(4)When a = 6, x = 3,

x++

=> x = x + 1

=> x = 3 + 1 = 4

a+=x

=> a = a + x

=> a = 5 + 4 = 10

(5)When a = 10, x = 4,

x++

=> x = x + 1

=> x = 4 + 1 = 5

a+=x

=> a = a + x

=> a = 10 + 5 = 15

(6)When a = 15, x = 5,

x++

=> x = x + 1

=> x = 5 + 1 = 6

a+=x

=> a = a + x

=> a = 15 + 6 = 21

(7)When a = 21, x = 6,

x++

=> x = x + 1

=> x = 6 + 1 = 7

a+=x

=> a = a + x

=> a = 21 + 7 = 28

(8)When a = 28, x = 7,

x++

=> x = x + 1

=> x = 7 + 1 = 8

a+=x

=> a = a + x

=> a = 28 + 8 = 36

(8)When a = 36, x = 8,

x++

=> x = x + 1

=> x = 8 + 1 = 9

a+=x

=> a = a + x

=> a = 36 + 9 = 45

(9) When a = 45, x = 9

then while (9<9)

condition not correct

So , loop closed .

Thank you . . .

If you like my Explanation then follow me !!

Similar questions