Home > Software engineering >  Loop does not continue correctly
Loop does not continue correctly

Time:03-02

I have been programming in C for the past 3 years, and I have always used the continue keyword in loops with success. But right now, a simple code of mine with continue is not working properly. Here is the code that is showing the problem:

int main()
{
    int num = 2, result = 0;
    while (num > 1 && num < 50)
    {
        if (num % 2 != 0)
        {
            result  = num;
        }
        else
        {
            continue;
        }
        num  ;
    }
    cout << "The result is: " << result << endl;
    return 0;
}

As stated above, this does not print anything on the console. When I remove the else statement with continue in it, it successfully calculates the result and prints the said result. Does anyone have any idea why the given code is not working correctly (or why the loop in it does not break)? Any sound answer would be much appreciated.

CodePudding user response:

Loop is indeed continuing (continue works properly) correctly

Initially, num = 2, so if condition fails and goes to else. So it will call continue. Then again the loop starts from the beginning with num = 2. This continues forever.

In short, num value is not changing and if condition always fails.

CodePudding user response:

It's a very simple issue. Look at this block of code properly:

else
{
    continue;
}

Due to continue, n is never called because due to the non-changing value of n, num % 2 != 0 is always false. This is resulting in an infinite loop.

So to fix this issue, just remove the above block of code:

#include <iostream>

int main()
{
    int num = 2, result = 0;
    while (num > 1 && num < 50)
    {
        if (num % 2 != 0)
        {
            result  = num;
        }
        /*else
        {
            continue;
        }*/
        num  ;
    }
    std::cout << "The result is: " << result << std::endl;
    return 0;
}

Also, consider not using the following in your code:

using namespace std;

..as it's considered as a bad practice. For more info on this, look up why is "using namespace std" considered as a bad practice.

CodePudding user response:

You just have to remove your else part because of no use and terminated cause of continue keyword.

int main()
{
    int num = 2, result = 0;
    while (num > 1 && num < 50)
    {
        if (num % 2 != 0)
        {
            result  = num;
        }
        num  ;
    }
    std::cout << "The result is: " << result << std::endl;
    return 0;
}

CodePudding user response:

Since continue is just a thinly disguised goto, rewriting the loop with explicit gotos might make it clearer:

  start:
    if (num > 1 && num < 50)
    {
        if (num % 2 != 0)
        {
            result  = num;
        }
        else
        {
            goto start;
        }
        num  ;
        goto start;
    }

Now you see clearly that num isn't incremented when num % 2 != 0 is false.

Just remove the else branch.

  •  Tags:  
  • c
  • Related