The Wayback Machine - https://waybackassets.bk21.net/20150918014407/http://stackoverflow.com:80/questions/32615717/converting-a-for-loop-with-continue-statement-to-while-loop
Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am working on an exercise where a small piece of code based on a for-loop is converted to preform the same operation with a while loop. The conversion is wrong by 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 is converted into:

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 am asked to explain why the conversion is wrong and then fix it. My thoughts are:

  1. With the initial value of i = 0, this will trigger continue instantly after entering the will loop, since (0 % 3 == 0) will make the if-statement true. As long as the initial value is 0, this will execute the loop, for so to skip it an endless amount of times. I tried changing the initial value of i = 1, but noe sum is printed. Then I tried to increment i before executing the if-statement, and the program now prints the sum 7. The question here is; why won't the program print if i incremented after the if statement, even if the initial value of i = 1 suggests (in my head) that program should run properly?

  2. I made a table for each program to compare the summing.

The for-loop version:

i = 0, sum += i not preformed (continue), i++

i = 1, sum = 1, i++

i = 2, sum = 3, i++

i = 3, sum += i not preformed (continue), i++

i = 4, i < 4 false, loop stopped

The while-loop version:

i = 0, i++, sum = 1

i = 1, i++, sum = 3

i = 2, i++, sum += i not preformed (continue)

i = 3, i++, sum = 7

i = 4, i < 4 false, loop stopped

In the while-loop, sum += i is preformed once more than in the for-loop. Is this the right way to convert the for-loop into a while-loop?

int i = 0, sum = 0;

        while (i < 3) {
            i++;
            if (i % 3 == 0) continue;
            sum += i;           
        }

        System.out.println(sum);
share|improve this question
    
for 1. 3 % 3 is zero –  RC. yesterday
    
Strictly speaking there is another difference: i is now visible from the rest of the block. An equivalent of a for(int i=0;....) {} construct is { int i = 0; while (...) {} } (note the extra pair of braces enclosing both the variable declaration and the loop). –  biziclop yesterday

6 Answers 6

up vote 3 down vote accepted

The for-loop given by the question if converted to plain English, it means sum up from 0 to 3 and exclude 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 up 0 to x and exclude all multiples of 3 using a while-loop. We will do this without looking at the original for-loop. (Sometimes it is easier to do it this way since we already know the intention of the for-loop)

To sum up a number from 0 to 3:

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

To sum a number from 0 to 3, excluding multiples of 3:

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

To sum a number from 0 to 3, excluding multiples 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 the continue, all statements below it will be skipped, you have to remember to do an increment i++ before calling continue. There will be another i++ since you branch out into 2 conditions now.

share|improve this answer

Your 1 is focussing on it being the initial value, but that's not the point. The point is that i is never incremented when i % 3 == 0 is true, not that 0 is the initial value. So the while loop loops forever.

Your 2 doesn't make any sense: The while version will loop forever, never summing anything.

Is this the right way to convert the for-loop into a while-loop?

No, you're incrementing i at the wrong time.

Think bout how a for loop works:

  1. Initialization - First it sets the variables to the values in the first expression.

  2. Test - Then it tests the values using the second expression.

  3. Execute - If the value is true, it executes the loop body.

  4. Increment - When the loop body is complete, it executes the third (increment) expression. Note that this is after the loop body.

Make your while loop do what the for loop is doing. (I'm intentionally not doing that for you; this is a learning exercise. I'll note that the best way to convert that for loop will not use continue, however. "Best," of course, is subjective.)

share|improve this answer

In the exercise, I am asked to explain why the conversion is wrong and then fix it

The conversion is wrong simply because when you will reach a i value that modulo 3 equals 0 (the first iteration in that case), you will skip to the next iteration and re-validate. However, since you skipped directly without incrementing i , you will re-validate the same condition and re-validate ad-infinitum.

You could fix it easily by getting rid of the continue and negating the condition :

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

    i++;    
}
share|improve this answer

According to the while loop logic, increment of variable i is conditional where as for for-loop it is unconditional. To make the increment logic uniform, you should increment in both cases.

I think the proper converting would be -

int i = 0, sum = 0;

while (i < 4) {
    if (i % 3 == 0) {i++;continue;}
    sum += i;   
    i++;   
}
System.out.println(sum);
share|improve this answer
    
This could be simplified, you're incrementing in and outside the if why not always increment ? The continue simply have nothing to do here. –  Jean-François Savard yesterday
    
Hay @Jean-FrançoisSavard, yes we can only thing that would break the conversion uniformity –  Subhrajyoti Majumder 20 hours ago

Normally the increment would be on the last line of a while loop. However, in this "disaster waiting to happen" code, there is a condition to skip to the end of the while block without incrementing i. If you are relying on i to increment, make sure it is always executed on any iteration. And also, at the end, because you want it to iterate the first time.

    while (i < 4) {
        if (i % 3 == 0) {
          // skip!
        } else {
          sum += i;   
        }
        i++;    
    }
share|improve this answer

The key difference between a for-loop and a while-loop in Java is that: you can have an increment being done for you in a for-loop.

For example:

for(int i=0; i<4; i++)

However, in a while-loop. The increment has to be coded explicitly somewhere within the loop.

For example:

int i=0;

while(i<4) { i++; }

So doing a conversion from for-loop to a while-loop. Always ensure the place where you placed the increment is always reachable. That means, in every iteration, ensure an increment is successfully performed.


In your attempt, you placed the increment at the first line of the while-loop. While this ensures an increment is always performed, however placing increment at the top has dire problems.

It will affect all the operations below, causing your calculations to go wrong.

So instead of placing i++ as the first line, you should shift it to the last line inside your while-loop.

    while (i < 3) {
        if (i % 3 == 0) {i++; continue;}  //i++ also needed here, as the bottom i++ can't be reached after continue
        sum += i;
        i++;   //move it here           
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.