Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each of the following indefinite loops, state how many iterations it will pe

ID: 3631649 • Letter: F

Question

For each of the following indefinite loops, state how many iterations it will perform (i.e., how many times its body will be executed). If it is an infinite loop or if it performs no iterations, say so.


i. int x = 2;
while (x < 16) {
System.out.println(x);
x += 2;
}
ii. int num = 5;
while (num != 0) {
num = num - 2;
}

iii. int num = 2;
do {
num--;
} while (num > 2);

iv. int num = 2;
while (num > 2) {
num--;
}

The following method "counts down" from a number n, printing the integers from n down to 1:
public static void countDown(int n) {
for (int count = n; count >= 1; count--) {
System.out.println(count);
}
}
For example, countDown(5) would print
5
4
3
2
1
If n is less than or equal to 0, the method does not print anything. Rewrite this method so that it uses a while loop instead of a for loop.

Explanation / Answer

int x = 2;
while (x < 16) {
System.out.println(x);
x += 2;
}

This loop will work until x becomes larger than or equal to 16.
and x is initially 2 and incrementing by 2 at each iteration.
So first the compiler will go to while statement and a comparison will be made.
for the condition (2<16) [because x is initially 2.]
It will return 1 and so the loop will be executed and on the screen 2 will be printed.
Then in the second line of the loop the following equation will execute
x = x+2
since x = 2
so x now becomes x+2 = 2+2 = 4.
again when the compiler reaches the closed braces { it returns to the while statement and again checks for the condition (4<16). [because x is now 4]
and this continues till x becomes 16.
Now the compiler makes the comparison (16<16) and returns 0 because this is false and consequently loop is not executed.
Therefore the loop continued till x=14. After that it became 16 and the condition under while statement became false.
So if we now check on which iteration x becomes 14 we can calculate the no. of times this loop will execute.
so initally x=2
at each iteration x inreases by 2.
i.e. after 2nd iteration x=4
after 3rd iteration x=6
after 4th iteration x=8
So its obvious that x will become 14 in the 7th iteration
So the no. of times this loop executes is 7.



ii. int num = 5;
while (num != 0) {
num = num - 2;
}

This loop will work until num becomes equal to 0.
and num is initially 5 and decreasing by 2 at each iteration.
So first the compiler will go to while statement and a comparison will be made.
for the condition (5 != 0) [because num is initially 5.]
It will return 1 and so the loop will be executed .
Then in the second line of the loop the following equation will execute
num = num-2
since num =5
so num now becomes num = num -2
= 5 - 2
= 3
again when the compiler reaches the closed braces { it returns to the while statement and again checks for the condition (3 != 0 ). [because num is now 3]

It will return 1 and so the loop will be executed .
Then in the second line of the loop the following equation will execute
num = num-2
since num =3
so num now becomes num = num -2
= 3 - 2
= 1
again when the compiler reaches the closed braces { it returns to the while statement and again checks for the condition (1 != 0 ). [because num is now 1]


It will return 1 and so the loop will be executed .
Then in the second line of the loop the following equation will execute
num = num-2
since num =1
so num now becomes num = num -2
= 1 - 2
= -1
again when the compiler reaches the closed braces { it returns to the while statement and again checks for the condition (-1 != 0 ). [because num is now -1]

And this continues infinetly because now num can never be equal to zero. So it is an infinite loop.

iii. int num = 2;
do {
num--;
} while (num > 2);

In this loop num is initialized with value 2.
Then the compiler moves to the do statement and then enters the loop.
It now reaches the statement num--.
num-- decrements the value of num.
So when the compiler reaches the while statement it checks for the condition (1>2). [because num is now 1]
This is false statement and hence the loop terminates.
So this loop will execute once.


iv. int num = 2;
while (num > 2) {
num--;
}

In this loop when the compiler reaches the while statement it checks for the condition (2 >2 ). [because num is initialized with value 2]
This is false and returns 0. So loop is not executed even once.

The same function when written with while loop becomes:

public static void countDown(int n) {
int count = n;
while (count >= 1) {
System.out.println(count);
count--;
}
}

I hope this has resolved your queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote