55.0k views
5 votes
How many iterations for each for-loop where max = 10 and incr = 3 a. for (int i = 0; i < max; i++) b. for (int i = 0; i < max; i += 2) c. for (int i = 0; i < max; i += incr) d. for (int i = incr; i < max; i += incr) e. for (int i = max; i > 0; i--) f. for (int i = max; i < 0; i--) g. for (int i = 1; i < max; i *= incr) h. for (int i = 0; i < max; i *= incr)

1 Answer

4 votes

Step-by-step explanation:

Answer to the following iterations are:

a)

In the following for-loop, the loop will starts from 0 and end at 9.

for example:

0 1 2 3 4 5 6 7 8 9

So, the loop will iterate 0-9, which means 10 times.

b)

In the following for-loop, the loop will starts from 0 and end at 8 but in the gap of 2 because of the increment of 2 in the variable "i".

for example:

0 2 4 6 8

So, the loop will iterate even times or 5 times, which means 0 2 4 6 8.

c)

In the following for-loop, the loop will starts from 0 and end at 9 but in the gap of 3 because of the increment of 3 in the variable "i" and the value is assign in the variable "incr" is 3.

for example:

0 3 6 9

So, the loop will iterate 4 times, which means 0 3 6 9.

d)

In the following for-loop, the loop will starts from 3 and end at 9 but in the gap of 3 because of the increment of 3 in the variable "i" and the value is assign in the variable "incr" is 3.

for example:

3 6 9

So, the loop will iterate 3 times, which means 3 6 9.

e)

In the following for-loop, the loop will starts from 10 and end at 1 which means reverse order.

for example:

10 9 8 7 6 5 4 3 2 1

So, the loop will iterate 10-1, which means 10 times in reverse order.

f)

In the following for-loop, the loop will not works because of the condition which is i is less than 0 but in the following code loop will starts from 3.

So, that's why loop will not iterate.

g)

In the following for-loop, the loop will starts from 1 and end at 9 but in the multiple of 3 because the variable "i" will multiply by the variable "incr" and the value is assign in the variable "incr" is 3.

for example:

1 3 9

So, the loop will iterate 3 times, which means 1 3 9.

h)

In the following for-loop, the loop is the infinite loop because of the condition which is i is less than max but and the loop will starts from 0 and goes on infinite times.

So, that's why it is the infinite loop.

User Floriangosse
by
8.3k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.