104k views
0 votes
How many iterations will loops execute?

(a) for (int i = 0; i < 20; i+ +) { }
(b) for (int i = 1; i <= 20; i+ +) { }
(c) for (int i = 5; i < 20; i+ +) { }
(d) for (int i = 20; i > 0; i- -) { }
(e) for (int i = 1; i < 20; i = i + 2) { }
(f) for (int i = 1; i < 20; i *= 2) { }

User HiJump
by
5.7k points

1 Answer

6 votes

Answer:

(a)20

(b)20

(c)15

(d)20

(e)10

(f)5

Explanation:

(a) In this loop it start from "0" and iterated up to the value of i= "19" because there is 1 increment in each iteration i.e(i++) so total iterations will executed 20.

(b) In this loop it starts from "1" and iterated up to the value of i="20" because there is 1 increment in each iteration i.e(i++) so total iterations will executed 20.

(c) In this loop it start from "5" and iterated up to the value of i= "19" because their is 1 increment in each iteration i.e(i++) so total iterations will executed 15.

(d) In this loop, it starts from "20" and iterated up to the value of i= "1" because there is 1 decrement in each iteration i.e(i--) so total iterations will execute 20.

(e) In this loop, it starts from "1" and iterated up to the value of i="19" because there is 2 increment in each iteration i.e(i=i+2) so total iterations will execute 10.

(f) In this loop, it starts from "1" and iterated up to the value of i= "19" because there is twice the increment of value of i=i*2 so total iterations will executed 5

User Fedotoves
by
6.1k points