Difference between "no statement" and continuation

In the code below, please take a look at the part that has a continue statement. what difference would it make if I remove the continue statement and I don't put anything in its place.

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;
    }
}

      

+3


source to share


6 answers


It doesn't make any difference in your code, but consider something like this:

(Pseudocode):



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

    printf("%d", 10/i);
}

      

+5


source


It wouldn't make any difference. The operator continue

is a branch operator . Basically, it goes back to your loop without executing the code after it. Since the statement was continue

last executed in your loop, it has no effect.



+6


source


'continue' jumps straight to the end of the 'for' or 'while' brackets, "}". In your case, since there is nothing after the continue keyword anyway, it doesn't matter.

Just to be clear, there is usually 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 goes straight to the closing "}", ignoring code-3 and code-4. The next piece of code that runs here is code.

+3


source


There won't be any difference in your case.

However, the continuation will jump to the next iteration of the loop immediately and skip any code after it, if 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
}

      

Therefore, the continue statement may be useful in some cases, but it makes absolutely no difference to your code here (maybe depending on its compiler, but then it will only increase the size of the final executable by adding another jmp statement, or maybe not since it can turn off the loop completely).

+2


source


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;
        }
    }

      

Additional lines do not change the behavior of the code.

+2


source


The operator is continue

useless in your example. It is supposed to be used to bring code to the end of the 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

how to “evaluate the loop condition and continue the loop if the condition is false”, and you can think of break

how to “exit the loop ignoring the loop condition”.

Same with do-while

:

do
  {
    ...

    if (foo)
      continue;

    if (bar)
      break;

    ...

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

      

+2


source







All Articles