Today's learning is more difficult than a few days ago. We should not only understand the composition and structure of various statements, but also have the ability of logic programming. Because it is a little basic, it is not difficult to learn. Today, we also use circular statements to learn: factorial algorithm, digital factorial addition and dichotomy. We have a little sense of achievement in finding specific numbers in a regular array, Tomorrow is new year's Eve. The new year is a new year. The new year is 21 years old. In fact, I can't receive red envelopes from all my relatives anymore. I hope I can find a good job in the new year. I can give red envelopes to my relatives' children in the spring Festival next year.
Branch statement if else
In C language, non-zero is true
1. Grammatical structure:
If (expression) / / true talents execute downward
Statement;
If (1) expression;
else
Statement 2;
3. / / multi branch
If (expression 1)
Statement 1;
Else if (expression 2)
Statement 2;
else
Statement 3;
#includ<stdio.h> int main() { int a = 0; int b = 2; if(a == 1) if(b == 2) printf("hehe\n"); else printf("haha\n"); return 0; } //Cannot print because if else matches incorrectly // if else principle matching // correction //Proper use of {} can make the logic of the code clearer. //Code style is important #include<stdio.h> int main() { int a = 0; int b = 2; if(a == 1) { if(b == 2) { printf("hehe\n"); } } else { printf("haha\n"); } return 0; }
Branch statement switch
Switch (integer expression)
{
Statement item;
}
#include <stdio.h> int main() { int day = 0; switch(day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; } return 0; }
#include <stdio.h> int main() { int day = 0; scanf("%d",&day); switch(day) { case 1: case 2: case 3: case 4: case 5: printf("weekday\n"); break; case 6: case 7: printf("weekend\n"); break; default: printf("Input error\n"); break; } return 0; }
Loop statement while
In the cycle, as long as a break is encountered, all the later cycles will be stopped and the cycle will be terminated directly. So: break in while is used to permanently terminate the loop
#include <stdio.h> int main() { int i = 1; while(i<=10) { if(i == 5) break; printf("%d ", i); i = i+1; } return 0; } //Output 1234
Continue is used to terminate this cycle, that is, the code behind continue in this cycle will not be executed, but directly jump to the judgment part of the while statement. Determine the inlet of the next cycle.
#include <stdio.h> int main() { int i = 1; while(i<=10) { if(i == 5) continue; printf("%d ", i); i = i+1; } return 0; } //Dead cycle after output 1234
#include int main() { int ch = 0; while ((ch = getchar()) != EOF)// Put the characters read by getchar in ch, if not equal to EOF putchar(ch); //Output ch return 0; } The code here can be used to clean up the buffer with appropriate modifications
EOF - end of file flag
getchar();// Get a character
putchar(); // Output a character
Loop statement for
For (expression 1; expression 2; expression 3) loop statement;
Expression 1
Expression 1 is the initialization part, which is used to initialize the of the loop variable.
Expression 2
Expression 2 is a condition judgment part, which is used to judge the termination of the cycle.
Expression 3
Expression 3 is the adjustment part, which is used to adjust the loop conditions.
break and continue are similar in for, but continue may enter an endless loop in while, but only skip that loop in for
1. Do not modify the loop variable in the for loop 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; //The writing method of front closing and back opening for(i=0; i<10; i++) {} //Both sides are closed intervals for(i=0; i<=9; i++) {}
Some variants of the for loop
int i = 0; int j = 0; //If the initialization part is omitted, how many hehe are printed here? for(; i<3; i++) { for(; j<3; j++) { printf("hehe\n"); } } //Print 3 hehe inner layers, cycle 3 times, return to the outer layer and then enter the inner layer, j is still = 3
Loop statement do while
do
Circular statement;
While (expression);
#include <stdio.h> int main() { int i = 10; do { printf("%d\n", i); i++; }while(i<10); return 0; }
break and continue are similar to while. They will enter an endless loop, but the loop will be carried out at least once.