208k views
5 votes
How many times does the following loop body execute?int count = 52;for(int i = 0; i < count; i++){cout << count << endl;--count;}

26
52
25
None of the above

User Ivey
by
5.2k points

1 Answer

4 votes

Answer:

The correct option is(a) that is 26.

Step-by-step explanation:

In the for loop,"i" started with 0 and the initial value of "count" is 52.In the First

iteration it will print "52" as output. Then the value of "count" is decreased by 1 and become 51. So in the next iteration,"i" increased by 1 and become 1.then it will check i<count, this condition is true, so it will print 51.Similarly again count decreased by 1 and "i" increased by 1. This loop continue till i<count.When i become 26 then count become 26 the the condition "i<count" fails. so the loop will execute from 0-25 for value of "i". So the loop will execute 26 times only.

When we run this loop, we get 26 numbers from 52 -27.

52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27

this is the output of the given loop.

User Yixi
by
4.8k points