Summary of C language

  • 2020-07-21 09:24:57
  • OfStack

return, break and continue

These three keywords have one thing in common, that is, reading can make the following statement does not execute, the difference is to pick a different distance.

return is so powerful that if there is 1 return in 1 function and it executes, the function is dead. return represents the return from the called function to the calling function, with a return value specified by the arguments following return. return is usually necessary because the result of a function call is usually taken out by the return value. If a function does not need to return the result of the calculation, it often needs to return a status code to indicate whether the function is executing smoothly (-1 and 0 are the most commonly used status codes), the calling function can judge the execution of the called function by the return value.

break is slightly weaker by 1, which is generally used in loop statements to close the loop and execute from the outside of the current loop, ignoring any other statements and loop condition tests in the loop body. It can only break out of a 1 level loop, if your loop is a nested loop, then you need to step out using break at your nested level.

continue is similar to break in that it closes the loop, but it is not as powerful as break in that it can only end the loop, that is, it does not break out of the loop, but continues down to determine the loop condition to execute the statement. It can only end the loop once, but it cannot stop the loop to continue.

Let's look at an example:


int main()
{
 int i = 0;
 int n = 5;
 for (i = 0; i < 10; i++)
 {
  if (i == n)
  {
   return;
  }
  printf("hehe!\n");
 }
 printf("hehe!\n");
}

int main()
{
 int i = 0;
 int n = 5;
 for (i = 0; i < 10; i++)
 {
  if (i == n)
  {
   break;
  }
  printf("hehe!\n");
 }
 printf("hehe!\n");
}

int main()
{
 int i = 0;
 int n = 5;
 for (i = 0; i < 10; i++)
 {
  if (i == n)
  {
   continue;
  }
  printf("hehe!\n");
 }
 printf("hehe!\n");
}

These examples are shown by typing "hehe! And you can see the difference. return ends at the sixth run, and ends the function directly, so there will be five hehe runs. break also ends on the 6th, but it is the end of the loop, so there is another hehe out of the loop, a total of 6 times; continue ended the sixth cycle, so it performed 9 times inside the loop and 1 time outside the loop, for a total of 10 times.

return

1. Meaning: return means to return from the called function to the calling function, which can be accompanied by a return value, which can be a constant, variable or expression.

2, function: End the running function, and return the value of the function.

3. Function return value:

The calculated result indicates the smooth execution of the function (-1, 0).

Return values can be of various data types, such as: int, float, double, char, a, *a (pointer), structure or class (c++)

Above is this site this site tidy up all the content, hope to be able to give you help.


Related articles: