Foreword: in the last article, we have learned statements, branch statements and while loops. Next, let's learn do... While loops, for loops and goto statements. (since this article is consistent with the previous article, the article directory is set according to the content of the previous article)
3.2 for loop
We have learned the while loop before, so why use while to implement the loop?
Why are there three cycles? Can't one cycle meet our needs? Let's explain these problems one by one. First, let's take a look at the syntax of the for loop:
3.2.1 grammar
for(Expression 1; Expression 2; Expression 3) Circular statement;
Expression 1
Expression 1 is the initial part, which is used to initialize the loop variable.
Expression 2
Expression 2 is a condition judgment part, which is used to judge when the loop terminates.
Expression 3
Expression 3 is the adjustment part, which is used to adjust the loop conditions.
Practical problems:
Use the for loop to print numbers 1-10
#include <stdio.h> int main() { int i = 0; for (i = 1;//Initialization i < = 10// Judgment part i + + / / adjustment) { printf("%d ", i); } return 0; }
Execution flow chart of for loop:
Now let's compare the while loop with the for loop
int i = 0; //Achieve the same function i = 1;//Initialization part while (i <= 10)//Judgment part { printf("hehe\n"); i = i + 1; } //To achieve the same function, use while for (i = 1; i <= 10; i++) { printf("hehe\n"); }
It can be found that there are still three necessary conditions of the loop in the while loop, but the three parts may deviate far due to the problem of style
The search and modification are not centralized and convenient enough. Therefore, the style of for loop is better; The for loop is also used more frequently.
3.2.2 the role of break and continue in the for loop
We found that the functions of break and continue in the for loop are basically the same as those in the while loop, but there are still some differences.
//break int main() { int i = 0; for (i = 1; i <= 10; i++) { if (5 == i) break; printf("%d ", i); } return 0; } //continue int main() { int i = 0; for (i = 1; i <= 10; i++) { if (5 == i) continue; printf("%d ", i); } return 0; }
break:
continue:
3.2.3 loop control variables for loop
Recommendations:
1. Do not modify variables inside the for loop body to prevent the for loop from losing control.
2. It is suggested that the value of the loop control variable of the for statement should be written in "close before open interval".
int i = 0; for (i = 0; i < 10; i++) { } //Closed interval on both sides for (i = 0; i <= 9; i++) { }
3.2.4 some variants of the for loop
int main() { //Code 1 for (; ; ) { printf("hehe\n"); } //The initialization part, judgment part and adjustment part of the for loop can be omitted //However, it is not recommended to omit it at the beginning of learning, which is easy to cause problems //When the judgment part is omitted, the judgment result is true, and the code 1 is printed hehe in an endless cycle //Code 2 int i = 0; int j = 0; //How much hehe is printed here for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { printf("hehe\n"); } } //10 * 10 hehe //Code 3 int i = 0; int j = 0; //If the initialization part is omitted, how many hehe are printed here for ( ; i < 10; i++) { for ( ; j < 10; j++) { printf("hehe\n"); } } //10 hehe //Code 4 - use more than one variable to control the loop int x, y; for (x = 0, y = 0; x < 2 && y < 5; ++x, y++) { printf("hehe\n"); } //Two hehe return 0; }
3.2.5 one pen test question
//How many times does the cycle take int main() { int i = 0; int k = 0; for (i = 0, k = 0; k = 0; k++) { k++; printf("hehe\n"); } //k=0 is the assignment operator. 0 means false in C language, and the loop is not executed once return 0; }
3.3do... while() loop
3.3.1 grammar
do { sentence; }while();
3.3.2 execution flow chart
3.3.3 characteristics of do statement
At least once. The usage scenarios are limited, so it cannot be used frequently.
int main() { int i = 0; do { printf("%d ", i); i++; } while (i < 10); return 0; }
3.3.4 break and continue in do... while loop
int main() { int i = 0; do { if (5 == i) break; printf("%d ", i); i++; } while (i < 10); //0 1 2 3 4 return 0; } int main() { int i = 0; do { if (5 == i) continue; printf("%d ", i); i++; } while (i < 10); //0 1 2 3 4 dead cycle return 0; }
3.4 half search algorithm (binary search)
What is binary search?
In an ordered group of numbers, if you want to find a number from the beginning until you find it, you may need to find all the numbers in this group before you can find it, but it is much more convenient if you use binary search.
For example, the price of a dress is 1-100 yuan. Let's guess the price of the dress?
You certainly won't start with 1 2 3 4... This guess is too complicated, so we can use half search (binary search) to realize it. You can guess 50 first, and then half according to the prompt... You can find the answer soon.
Implementation example of binary search:
int main() { int arr[] = { 1,2,3,4,5,6,7,8,9 }; int k = 7; int sz = sizeof(arr) / sizeof(arr[0]); int left = 0; int right = sz - 1; while (left <= right) { int mid = left + ((right - left) / 2); if (arr[mid] > k) { right = mid; } else if (arr[mid] < k) { left = mid; } else { printf("Yes, the subscript is%d\n", mid); break; } } if (left > right) { printf("Can not find\n"); } return 0; }
4.goto statement
C language provides goto statements that can be abused and labels that mark jump.
In theory, goto statement is unnecessary, and it is easy to write code without goto statement in practice. However, goto statements are still useful in some situations. The most common usage is to terminate the processing of structures nested in some depth.
goto language is really suitable for the following languages:
for (...) { for (...) { if (disaster) goto error; } } ... error : if (disaster) // Handling error conditions //The following is a shutdown program #include <stdio.h> int main() { char input[10] = { 0 }; system("shutdown -s -t 60"); again: printf("The computer will shut down within 1 minute. If you enter: I'm a pig, cancel the shutdown!\n Please enter:>"); scanf("%s", input); if (0 == strcmp(input, "I'm a pig")) { system("shutdown -a"); } else { goto again; } return 0; }
Conclusion: the second chapter ends here. Thank you very much for your praise and support; Please give me three company!
If you want to know more about branches and loops, you can take a look at the implementation of the number game, which loop is used and which library functions are used?
Joy is accompanied by tears, success is accompanied by hardships, and regret inspires struggle.