How many iterations do the following loops carry out Assume

How many iterations do the following loops carry out? Assume that i is not changed in the loop body.

(a) for (int i = 1; i <= 10; i++)…

(b) for (int i = 0; i < 10; i++)…

(c) for (int i = 10; i > 0; i--)…

(d) for (int i = -10; i <= 10; i++)…

(e) for (int i = 10; i >= 0; i++)…

(f) for (int i = -10; i <= 10; i = i + 2)…

(g) for (int i = -10; i <= 10; i = i + 3)…

Solution

(a) for (int i = 1; i <= 10; i++)…

Answer) There are 10 iterations are carry out by the above loop

Output: 1 2 3 4 5 6 7 8 9 10

(b) for (int i = 0; i < 10; i++)…

Answer) There are 10 iterations are carry out by the above loop

Output:  0 1 2 3 4 5 6 7 8 9

(c) for (int i = 10; i > 0; i--)…

Answer) There are 10 iterations are carry out by the above loop

Output:10 9 8 7 6 5 4 3 2 1  

(d) for (int i = -10; i <= 10; i++)…

Answer) There are 21 iterations are carry out by the above loop

Output: -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10

(e) for (int i = 10; i >= 0; i++)…

Answer) There are infinite number of iterations are carry out by the above loop

Output: 10 11 12 13 14 15 16 17 18 19 20 ........so on

(f) for (int i = -10; i <= 10; i = i + 2)…

Answer) There are 11 iterations are carry out by the above loop

Output: -10 -8 -6 -4 -2 0 2 4 6 8 10

(g) for (int i = -10; i <= 10; i = i + 3)…

Answer) There are 7 iterations are carry out by the above loop

Output: -10 -7 -4 -1 2 5 8

How many iterations do the following loops carry out? Assume that i is not changed in the loop body. (a) for (int i = 1; i <= 10; i++)… (b) for (int i = 0; i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site