-1

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.

  • 1
    Think it through (on paper), what value will `num` have after the `continue` is executed? And how will it ever be able to break out of the loop then? – UnholySheep Mar 01 '22 at 11:25
  • 1
    So given any even number in 2...49 inclusively, this loops forever. Hint: `continue` skips the rest of the loop, including your increment, and just starts at the conditional again, where `num` has not changed, is still in 2...49, is still even, and you'll still `continue` ad-infinitum. – WhozCraig Mar 01 '22 at 11:25
  • "this does not print anything on the console." Would it be correct to describe your observation as "does not terminate, while printing nothing"? – Yunnosch Mar 01 '22 at 11:30
  • Simple programming bugs like these is exactly why knowing how to effectively use a debugger is a mandatory skill for every C++ developer. Using a debugger to run the shown program, one line at a time: even the worst debugger in the world would've shown, step by step, how the loop keeps repeating the same statements, and nothing ever changes. After investing a little bit of time in learning how to use a debugger it becomes possible to figure out these kinds of Scooby-Doo mysteries, in your own code, all by yourself and without needing anyone's help. Does that make sense? – Sam Varshavchik Mar 01 '22 at 11:38
  • "Always used `continue` effectively in the past" -- the most effective uses of keywords that break control flow -- continue, break, goto -- is to not use them at all. (Other than break in a switch statement.) Even if you perfectly know what you're doing, these are a finicky bunch that makes certain things like refactorings harder than necessary. – DevSolar Mar 01 '22 at 11:44
  • Okay, thank you, everyone, for your help and time to answer my question. – Hassan Mahmood Mar 02 '22 at 16:08

4 Answers4

4

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.

j23
  • 3,139
  • 1
  • 6
  • 13
1

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.

The Coding Fox
  • 1,488
  • 1
  • 4
  • 18
0

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;
}
Vikash Kumar
  • 80
  • 2
  • 12
0

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82