Sibs
Sibs

Reputation: 91

How to convert this for loop to a while loop in C using a continue statement

I am trying to convert the following for-loop

for( x = 1; x <= 10; x++ ){

    if( x == 5 ){
        continue;
    }

    printf("%d ", x);

}

into a while-loop. However, the following code, which seems reasonable to me, is not executing properly:

int x = 1;
while( x <= 10 ){
    if( x == 5){
       continue;
    }
    printf("%d ", x);
    x++;
}

Why is this the case?

Thanks in advance.

Upvotes: 0

Views: 226

Answers (6)

Halina V
Halina V

Reputation: 49

You just need to add "else" - what you want to be done if the "if statement" is not working, or you can tell the program what to do in every other case:

int x = 1;
while( x <= 10 ){
    if( x != 5)
       printf("%d ", x);
    x++;
}

Upvotes: -1

NamoDawn
NamoDawn

Reputation: 26

Welcome to C! If you can keep something simpler without compromising efficiency and readability, do that _(^.^)_/ ! i.e.

int x = 1;
while( x <= 10 ){
    if( x != 5)
        printf("%d ", x);
    x++;
}

Notice: You don't even need brackets if you're doing a single command [within that "if" statement).

When you use "continue" in a while, you are skipping the loop's instructions, and you need 'em in order to iterate! ;)

Upvotes: 1

Michael Geary
Michael Geary

Reputation: 28850

Here's a version of your for loop in JavaScript so we can run it here and see the output. C and JavaScript have the same loop semantics, so this makes a handy way to test the code:

for( x = 1; x <= 10; x++ ) {

    if( x == 5 ) {
        continue;
    }

    console.log( x );
}

Now, why doesn't your while loop produce this output? It's because of the continue statement. Here's a JavaScript version of your while loop:

var x = 1;
while( x <= 10 ) {

    if( x == 5) {
       continue;
    }

    console.log( x );

    x++;
}

On second thought, don't run that! In fact I took off the snippet tag so you don't run it. Because here is the problem...

When you use continue in a any kind of loop, it skips the rest of the loop body. In the for loop, the x++ is not part of the loop body. It is part of the for statement itself, so it gets run even when you use continue inside the loop. In the while loop, the x++ is part of the loop body, so it gets skipped along with the rest of the body.

Your while version will hit an infinite loop when x is 5, because it will not increment x during that loop iteration, and in fact will never increment x again.

Now for some advice: it is certainly possible to write a while loop that works like the corrected for loop, but it's going to be a bit more complicated. Why do you want to use while instead of for? Stick with the for loop for simplicity, or explain the reason you want to use while instead.

In general, you can say that the following for loop and while loop are equivalent:

for( a; b; c ) {
    // loop body
}

a;
while( b ) {
    // loop body
    c;
}

But they are not the same when you use continue in the loop body, for the reason mentioned above.

If you really want to use while, you can use a goto as in Amadeus's answer. Or you can rearrange the loop body, like this:

var x = 1;
while( x <= 10 ) {

    if( x != 5) {
        console.log( x );
    }

    x++;
}

Upvotes: 2

hawkeye
hawkeye

Reputation: 169

In the second block of code you have

int x = 1;
while( x <= 10 ){
    for( x == 5){
       continue;
    }
    printf("%d ", x);
    x++;
}

and in the second line of the while statement, you have for(x == 5) instead of if (x == 5) like you have in the for-loop in the first block.

Otherwise, this is the correct way to write a for-loop as a while loop, essentially move the initialization variable to outside of the loop, and place the modification statement at the end. Depending on the situation, one will probably be a better choice than the other, but they are interchangeable.

Edit:

It would probably be best to minimize the code by excluding the one time the value is 5 instead of using continue when it isn't. Additionally, the other poster is correct, your continue statement would exclude the increment of x causing an infinite loop.

int x = 1;
while( x <= 10 ){
    if ( x != 5){
       printf("%d ", x);
    }
    x++;
}

Upvotes: 0

Amadeus
Amadeus

Reputation: 10655

A for loop can be seen as compound of:

for (initialization; test; increment) 
{
    body
}

and can be implemented using while loop keeping this format:

initialization;
while (test)
{
    body
    increment;
}

So, in your case, the solution is (you are using the continue statement, which means that you have to skip the rest of the body and goes directly to the increment part):

x = 1;
while (x <= 10) {
    if( x == 5 ){
        goto cont;
    }

    printf("%d ", x);

    cont: x++; // this increment is due the increment in for statement
}

Using goto to jump to skip the body and going to the increment part

Upvotes: 1

TonyB
TonyB

Reputation: 947

Each iteration (except when X==5) has two iterations, one in the for() and one in the body. Try something like this:

int x = 1;
while( x <= 10 ){
    if( x == 5){
      ;
    }else{
        printf("%d ", x);
        x++;
    }
    x++ // "for()" increment
}

Upvotes: 0

Related Questions