Convert for-loop with continue statement to while-loop

I am working on an exercise where a small piece of code based on a for-loop is converted to storing the same operation with a while loop. The conversion is incorrect for the purpose and looks like this:

int sum = 0; for (int i = 0; i < 4; i++) { if (i % 3 == 0) continue; sum += i; } System.out.println(sum); // prints 3 

This translates to:

 int i = 0, sum = 0; while (i < 4) { if (i % 3 == 0) continue; sum += i; i++; } System.out.println(sum); // will not print 

In the exercise, I will be asked to explain why the transformation is wrong, and then fix it. My thoughts:

  • With the initial value i = 0, this will immediately cause continue to continue after entering a wait loop, since (0% 3 == 0) will make if-statement true. As long as the initial value is 0, this will lead to a loop, so skip it an infinite number of times. I tried to change the initial value i = 1 , but noe sum is printed. Then I tried to increase i before the if-statement was executed, and now the program prints the sum 7. The question is: why the program will not be printed if I increased after the if statement, even if the initial value i = 1 tells (in my head) that the program should work right?

  • I made a table for each program to compare summation.

Version for the loop :

i = 0, sum + = I have not completed (continued), i ++

i = 1, sum = 1, i ++

i = 2, sum = 3, i ++

i = 3, sum + = I have not completed (continued), i ++

i = 4, i <4 false, stop stopped

Version of the while-loop:

i = 0, i ++, sum = 1

i = 1, i ++, sum = 3

i = 2, I ++, sum + = I have not completed (continued)

i = 3, i ++, sum = 7

i = 4, i <4 false, stop stopped

In the while-loop, the sum + = i is executed again than in for the loop . Is this the correct conversion method for a loop in a while-loop ?

 int i = 0, sum = 0; while (i < 3) { i++; if (i % 3 == 0) continue; sum += i; } System.out.println(sum); 
+6
source share
5 answers

for the cycle asked by the question, if it is converted to plain English , this means summing up from 0 to 3 and excluding all multiples of 3 . (0 + 1 + 2 = 3)

  for (int i = 0; i < 4; i++) { if (i % 3 == 0) continue; sum += i; } 

So now we ask ourselves: how do we sum 0 to x and exclude all multiples of 3 using a while-loop . We will do this without looking at the source for the loop . (Sometimes it's easier to do so, since we already know the for-loop intent)

Bring a number from 0 to 3:

 int i = 0; while(i < 4){ sum += i; i++; } 

To sum a number from 0 to 3 excluding the multiple of 3:

 int i = 0; while(i < 4){ if(i % 3 != 0) sum += i; i++; } 

To sum a number from 0 to 3, excluding the multiple of 3 (using continue):

 int i = 0; while(i < 4){ if(i % 3 == 0){ i++; //Remember to increase i before continue continue; } else sum += i; i++; } 

Since after continue all the instructions below will be skipped, you must remember to do an i++ increment before calling continue . There will be another i++ , since you are now deployed to 2 conditions.

+3
source

Your 1 focuses on the fact that this is an initial value, but that is not the point. The fact that i never increases when i % 3 == 0 is true, rather than 0 - is the initial value. So the while loop loops forever.

Your 2 makes no sense: the while version will loop forever, never summing anything up.

Is it right to convert a for-loop into a while loop?

No, you increase i at the wrong time.

Think about how the for loop works:

  • Initialization. First, it sets the variables to the values ​​in the first expression.

  • Test He then checks the values ​​using the second expression.

  • Run - if the value is true, it executes the body of the loop.

  • Increment. When the body of the loop is completed, it executes the third (increment) expression. Please note that this is after the body cycle.

Create a while that executes the for loop. (I purposely do not do it for you, this educational uprazhnenie.Ya zametlyu that the best way to convert this cycle for will not use the continue , however. "The Best", of course, subjective.)

+5
source

In the exercise, I will be asked to explain why the transformation is wrong, and then fix it

The conversion is incorrect, because when you reach the value of i , which modulo 3 is 0 (the first iteration in this case), you will go to the next iteration and repeat the check. However, since you skipped directly without increasing i , you re-confirm the same condition and re-confirm ad-infinitum.

You can easily fix this by getting rid of continue and denying the condition:

 while (i < 4) { if (i % 3 != 0) sum += i; i++; } 
+5
source

In accordance with the logic of the while loop, the increment of the variable i is conditional, where for the for-loop it is unconditional. To make the logic of increment uniform, you must increase in both cases.

I think the correct conversion would be -

 int i = 0, sum = 0; while (i < 4) { if (i % 3 == 0) {i++;continue;} sum += i; i++; } System.out.println(sum); 
+1
source

Usually the increment will be in the last line of the while loop. However, in this code “disaster is waiting to happen”, there is a condition to go to the end of the while block without increasing i. If you rely on me for augmentation, make sure it always runs at any iteration. And also, at the end, because you want it to iterate for the first time.

  while (i < 4) { if (i % 3 == 0) { // skip! } else { sum += i; } i++; } 
0
source

All Articles