continue statement-move to the beginning of the next loop

You can use the continue statement to move to the beginning of the next loop. It can be used in the block of for statement and while statement.

for (Initialize loop variable; Condition to loop; Update loop variable) {
  
  #Move to the next loop
  continue; continue;
}

while (condition to loop) {
  
  #Move to the next loop
  continue; continue;
}

This is a sample to move to the next loop by using the continue statement in the block of the for statement. It skips only when it is an even number and moves to the beginning of the next loop.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  
  // Find the total with a for statement
  int32_t total = 0;
  for (int32_t i = 0; i <10; i ++) {
    // Move to the next loop only if it is an even number
    if (i%2 == 0) {
      continue; continue;
    }
    
    total + = i;
  }
  
  printf("%d\n", total);
}

Output result

twenty five

Relationship between goto statement and continue statement

The continue statement is a kind of goto statement. You can think of it as a goto statement with a limited destination that moves to the beginning of the next loop.

In C language, it is a good practice to write a program without using goto statements as much as possible.

Use of continue statement and if statement properly

How do you use the continue statement and the if statement properly? You can also write the sample you wrote earlier with an if statement.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  
  // Find the total with a for statement
  int32_t total = 0;
  for (int32_t i = 0; i <10; i ++) {
    if (i%2! = 0) {
      total + = i;
    }
  }
  
  printf("%d\n", total);
}

In a nutshell, all programs can be written with a while statement and an if statement. The other syntax is syntactic sugar.

(Well, if you write it more strictly, you can write it only with goto and if statements. But well, I don't want to use goto. When it comes to lower level assemblers, simple jumps (corresponding to C language goto) and conditional jumps (Corresponding to the if statement in C language) is the most basic control instruction. The loop is written by conditional jumping to the position before the code.)

However, there are cases where it is better to use the continue statement to express a branch, in which case continue is used.

For example, if you want to treat only the beginning of a line as an exception and want to skip it.

while (condition) {
  // Skip if the line is at the beginning
  if (line == 1) {
    continue; continue;
  }
  
  // Main process
}

This can also be written in an if statement. The nesting of the main process is one deeper.

while (condition) {
  // Skip if the line is at the beginning
  if (line =! 1) {
    // Main process
  }
}

Both are the same from the computer's point of view, but if you want to emphasize the main process, you can write the exception by skipping the continue statement.

Is it a feeling that it is used properly for human sensitivity and ease of perception?

Differences from Perl in statements that move to the next loop

The C language continue statement corresponds to Perl's next statement.

Associated Information