3.5k views
0 votes
Examine the following while loops and determine the value of ires at the end of each of the loops and the number of times each loop executes.

(a) ires =1;
while mod(ires, 10) =0
ires = ires +1
end
(b) ires =2;
while ires ≪=200
ires = ires^ 2
end
(c) 1 res =2;
while ires >200
ires = ires^ 2 ;
end

1 Answer

4 votes

Final answer:

In loop (a), ires ends at 1 with 0 iterations. Loop (b) ends with ires at 256 after 3 iterations. Loop (c) ires remains at 2 with 0 iterations.

Step-by-step explanation:

The question involves analyzing while loops and finding out the final value of ires and the number of iterations for each loop.

  • (a) The loop will never execute because the initial value of ires does not satisfy the loop condition mod(ires, 10) = 0. Therefore, the value of ires remains 1, and the execution count is 0.
  • (b) This loop will execute until ires exceeds 200. Starting with ires = 2, it will square the value of ires each iteration (4, 16, 256). Once ires becomes 256, the condition is false, the loop ends, the final value is ires = 256, and the loop executes 3 times.
  • (c) The loop condition is never true since the initial value is 2 and does not satisfy ires > 200. Thus, the final value of ires remains 2, and the loop does not execute at all.

User Stram
by
8.9k points