3

In the code below, please take a look at the part which has the 'continue' statement. What difference would it make if I remove 'continue' statement and I put nothing in the place of it.

int prime_or_not(int x){
    int i;
    if (x == 1 || x == 2){
        return 1;
    }else if (x % 2 == 0){
        return 0;
    }else if (x > 2){
        for (i = 3; i < x; i += 2){
            if (x % i == 0){
                return 0;
                break;
            }else {
                continue;
            }
        }
        return 1;
    }else {
        return 0;
    }
}
Göksenin Cakir
  • 186
  • 1
  • 3
  • 9
  • 2
    What did you find out by single-stepping in the debugger? – Martin R Dec 31 '14 at 22:42
  • 3
    Neither the `break` nor the `continue` are necessary. – Jongware Dec 31 '14 at 22:42
  • 6
    Btw, 1 is *not* a prime number. – Martin R Dec 31 '14 at 22:42
  • 2
    there are some people who, in the name of "best practices" will **always** have an `else` for every `if`. In the code you show, the given `else { continue; }`, an empty `else {}`, and no `else` at all would all have the same effect. additionally, putting a break after the `return` is unreachable code. – Ryan Haining Dec 31 '14 at 22:47
  • 2
    regarding this line: 'for (i = 3; i < x; i += 2){' you could cut the max execution time in half by using: 'for (i = 3; i < (x>>1); i += 2){' – user3629249 Dec 31 '14 at 23:52
  • 2
    there is lot of unwanted statements present in your code. – Manjunath N Jan 01 '15 at 06:29

6 Answers6

7

It would make no difference at all. The continue statement is a jump statement. Basically, it jumps back to your loop not executing the code after it. Since the continue statement is last executed in your loop it has no effect.

Igor Pejic
  • 3,658
  • 1
  • 14
  • 32
6

It makes no difference in your code, but think about something like this:

(Pseudo code):

for(int i = 10; i > -5; i--) {
    if(i == 0)
        continue;

    printf("%d", 10/i);
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
4

'continue' jumps straight to the end of the 'for' or 'while' bracket, "}". In your case, since there is nothing after the continue keyword anyways, it makes no difference.

Just to make it clear, normally, there IS a big difference between 'continue' and no statement. For example,

for (code-condition){
 code-1
 code-2
 continue;
 code-3
 code-4
}

Once the 'continue' line is executed, it jumps straight to the closing "}", ignoring the code-3 and code-4. The next piece of code that gets executed here is code-condition.

Jobs
  • 3,317
  • 6
  • 26
  • 52
2

In your case there would be no difference.

However continue will move to the next iteration of the loop immediately and skip any code after it if there is any.

Consider this:

int x;
for(x = 0; x < 100; x++){
    if(x == 50){
        //do something special
        continue;
    }
    //code here is skipped for 50
    //do something different for every other number
}

So the continue statement can be useful in some instances but for your code here it makes absolutely no difference whatsoever(maybe depending on the compiler it will but then it would only increase the size of the end executable adding another jmp instruction, or maybe not since it may unwind the loop fully).

tom
  • 354
  • 4
  • 15
2

I would simplify the block

    for (i = 3; i < x; i += 2){
        if (x % i == 0){
            return 0;
            break;
        }else {
            continue;
        }
    }

to

    for (i = 3; i < x; i += 2){
        if (x % i == 0){
            return 0;
        }
    }

The additional lines don't change the behavior of the code.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

The continue statement is useless in your example. It is supposed to be used to move code execution to the end of a loop:

while (is_true)
  {
    ...

    if (foo)
      continue;

    if (bar)
      break;

    ...

    /* 'continue' makes code jump to here and continues executing from here */
  }
/* 'break' makes code jump to here and continues executing from here */

You can think of continue as "evaluate the loop condition and continue the loop if the condition is false", and you can think of break as "exit the loop, ignoring the loop condition".

Same thing with do-while:

do
  {
    ...

    if (foo)
      continue;

    if (bar)
      break;

    ...

    /* 'continue' moves execution to here */
  }
while (is_true);
/* 'break' moves execution to here */