catalogue
Knowledge point 1 [if statement] (important)
1. If statement: a certain result is true. Please select if
Case: keyboard input an int data can be divided by 3 and output ok
Case: keyboard input an int data can be divided by 3, output ok, otherwise output no
3,if....else if....else if....else.....
Case 1: enter an int data on the keyboard to find the remainder of pair 3
4,if if if ..... Each if is independent
Knowledge point 2 [switch statement] (important)
Case: part of break can be omitted
Knowledge point 3 [for loop statement] (important)
Case 1: sum of 0 ~ 100 statistics
Knowledge point 4 [while loop statement] (important)
Knowledge point 5 [do...while loop statement] (important)
Knowledge point 6 [goto jump statement] (understand)
Knowledge point 1 [if statement] (important)
1. If statement: a certain result is true. Please select if
if(Expression 1) { Statement 1; }
If expression 1 is true, execute statement 1;
Case: keyboard input an int data can be divided by 3 and output ok
void test00(){ int num = 0; printf("Please enter a int Type data:\n"); scanf("%d", &num); if(num%3 == 0){ //Can be divided by three printf("ok\n"); } } int main(int argc, char const *argv[]) { return 0; }
Be careful not to add a semicolon ';' after the if statement, Otherwise, the contents in the following '{}' will be executed regardless of whether the judgment is correct or not
if(num%3 == 0);{ //Can be divided by three printf("ok\n"); }
2,if....else.....
If the project has two results and the two results will not appear at the same time, please select if else......
if(Expression 1) { Statement 1; } else//No conditions { Statement 2; }
If expression 1 is true, execute statement 1, otherwise execute statement 2
Case: keyboard input an int data can be divided by 3, output ok, otherwise output no
void test00(){ int num = 0; printf("Please enter a int Type data:\n"); scanf("%d", &num); if(num%3 == 0){ //Can be divided by three printf("ok\n"); } else{ printf("no\n"); } }
3,if....else if....else if....else.....
If the project has multiple results, but only one will appear at the same time.
Note: the last else may not be written
if(Expression 1) { Statement 1; } else if(Expression 2) { Statement 2; } else if(Expression 3) { Statement 3; } else Can be omitted { Statement 4; }
Expression 2 is judged only when expression 1 is not true, expression 3 is judged only when expression 2 is not true, and the statements in else will be executed only when the above expressions are not true
If expression 1 is true, execute statement 1, and the following expressions will not jump out of the if statement
Case 1: enter an int data on the keyboard to find the remainder of pair 3
void test02() { int num = 0; printf("Please enter a int data:"); scanf("%d", &num); if(num%3 == 0) { printf("%d The remainder of 3 is:0\n", num); } else if(num%3 == 1) { printf("%d The remainder of 3 is:1\n", num); } else if(num%3 == 2) { printf("%d The remainder of 3 is:2\n", num); } }
4,if if if ..... Each if is independent
When the project has multiple results, but the results will be uncertain at the same time
if(Expression 1) { Statement 1; } if(Expression 2) { Statement 2; }
The above two if are completely independent of each other
Knowledge point 2 [switch statement] (important)
switch((expression)//Expression can only be character or integer (short int long) { case Constant expression 1://Constant expressions cannot have variables Statement 1; break; //Try to have a break for each case case Constant expression 2: Statement 2; break; default: Statement 3; break; }
Compare the value of the expression with the value corresponding to the case one by one. The program will enter from the case statement and jump out of the switch statement when it encounters a break.
void test01(){ int num = 0; printf("Please enter a int Type data:\n"); scanf("%d", &num); switch (num %3){ case 0: printf("%d The remainder for 3 is: 0\n", num); break; case 1: printf("%d The remainder of 3 is: 1\n", num); break; case 2: printf("%d The remainder of 3 is 2\n", num); break; default: break; } }
If the program enters from constant expression 1, it will execute statement 1. If it does not encounter a break, it will continue to execute statement 2 and will not judge constant expression 2
void test01(){ int num = 0; printf("Please enter a int Type data:\n"); scanf("%d", &num); switch (num %3){ case 0: printf("%d The remainder for 3 is: 0\n", num); //If there is no break, the program will continue to execute the next item case 1: printf("%d The remainder of 3 is: 1\n", num); break; case 2: printf("%d The remainder of 3 is: 2\n", num); break; default: break; } }
If switch can be implemented, then if can definitely be implemented.
If your project result is a single value instead of a range, not a string and a real type, it is recommended to choose switch
If the result of your project is range, string and real type, it is recommended to select if
Case: part of break can be omitted
Keyboard input qwer skills:
void test04() { while(1) { printf("Please enter your skills qwer:"); char ch = '\0'; //ch = getchar(); //getchar needs to enter to get keyboard input //getchar();// Clear carriage return ch = getch();//Get keyboard input that does not require enter confirmation switch(ch) { case 'q': case 'Q': printf("silent\n"); break; case 'w': case 'W': printf("defense\n"); break; case 'e': case 'E': printf("rotate\n"); break; case 'r': case 'R': printf("Big sword\n"); break; default: printf("Invalid skill\n"); break; } } }
Knowledge point 3 [for loop statement] (important)
for(Conditional initialization statement ; Cycle condition ; Step statement) { Circulatory body; }
Initialization statement: it will be executed only once before the first loop
Cycle condition: only when the cycle condition is true will it enter the cycle body
Step statement: the statement to be executed after each execution of the loop body
Loop body: what needs to be done in a loop
Case 1: sum of 0 ~ 100 statistics
void test02(){ int i; int sum; for(i=0, sum=0; i<=100; i++){ sum = sum + i; } printf("sum = %d\n", sum); } int main(int argc, char const *argv[]) { test02(); return 0; }
void test02(){ int i = 0; int sum = 0; for(; i<=100; i++){ sum = sum + i; } printf("sum = %d\n", sum); }
The break in the for loop is to jump out of the loop.
void test02(){ int i = 0; int sum = 0; for(;; i++){ sum = sum + i; if(i == 100){ break; } } printf("sum = %d\n", sum); }
continue in for ends this cycle and directly enters the next cycle
void test02(){ int i = 0; int sum = 0; for(i=0; i<=100; i++){ if(i == 50){ //When i=50, skip this cycle and enter the next cycle continue; } sum = sum + i; } printf("sum = %d\n", sum); }
Knowledge point 4 [while loop statement] (important)
Initialize the variables in advance; while(Cycle condition) { Circulatory body; Remember to write step statements; }
If the cycle condition is true, enter the cycle, otherwise exit the cycle
void test03(){ int i = 0; int sum = 0; while(i <= 100){ sum += i; i++; } printf("sum = %d\n", sum); } int main(int argc, char const *argv[]) { test03(); return 0; }
break out of while
void test03(){ int i = 0; int sum = 0; while(1){ //Dead cycle sum += i; if(i == 100){ break; } i++; } printf("sum = %d\n", sum); }
continue ends the current cycle and enters the next cycle
void test03(){ int i = 0; int sum = 0; while(i <= 100){ sum += i; if(i == 50){ i++; continue; } i++; } printf("sum = %d\n", sum); }
Loop nested loop:
while nesting
void test03(){ int i = 0; int j = 0; while(i <= 5){ //The outer cycle is executed 5 times printf("i = %d\n", i); //5 times in total j = 1; while(j <= 3){ //The inner loop is executed 3 times printf("\tj = %d\n", j); //15 times in total j++; } i++; } } int main(int argc, char const *argv[]) { test03(); return 0; }
for nesting
void test03(){ int i = 0; int j = 0; for(i=1; i<=5; i++){ printf("i = %d\n", i); for(j=1; j<=3; j++){ printf("\tj = %d\n", j); } } } //One outer loop and three inner loops //Five outer cycles and 15 inner cycles int main(int argc, char const *argv[]) { test03(); return 0; }
break can only jump out of the loop closest to it;
void test03(){ int i = 0; int j = 0; for(i=1; i<=5; i++){ printf("i = %d\n", i); for(j=1; j<=3; j++){ if(j == 2) break; //Jump out of inner loop every time j==2 printf("\tj = %d\n", j); } } } /** i = 1 j = 1 i = 2 j = 1 i = 3 j = 1 i = 4 j = 1 i = 5 j = 1 */
Knowledge point 5 [do...while loop statement] (important)
do { Circulatory body; }while(Cycle condition);
Execute the loop body once, and then judge the loop condition. If it is true, continue the loop. If it is false, exit the loop
void test04(){ int i = 0; while (i>0){ printf("A loop\n"); } do{ printf("B loop\n"); }while(i>0); } int main(int argc, char const *argv[]) { test04(); return 0; }
Knowledge point 6 [goto jump statement] (understand)
Use as little as possible, which is easy to destroy the encapsulation of functions
void test05(){ printf("-----001------\n"); printf("-----002------\n"); goto here; printf("-----003------\n"); printf("-----004------\n"); printf("-----005------\n"); here: printf("-----006------\n"); } int main(int argc, char const *argv[]) { test05(); return 0; }
void test05(){ printf("-----001------\n"); printf("-----002------\n"); here: //Dead cycle printf("-----003------\n"); printf("-----004------\n"); printf("-----005------\n"); goto here; printf("-----006------\n"); } int main(int argc, char const *argv[]) { test05(); return 0; }