catalogue
if statement and switch statement
In this way, we first summarize several forms of if statements
if and else appear simultaneously;
There is also an embedded if statement;
Let's look at some variants of the for loop
Branch and loop statements
Branch statement
if statement and switch statement
Let's first look at what a statement is.
#include"stdio.h" int main() { int a; int b;//Separated by semicolons are statements }
In this way, we first summarize several forms of if statements
Single if statement:
if { printf(""); }
if and else appear simultaneously;
if { printf(""); } else { printf(""); }
If and if else
if { printf(""); } if else { printf(""); } else { printf("");//Statements are executed sequentially. }
There is also an embedded if statement;
if(Conditions met) { if { printf(""); } else { printf(""); } }
swtich statement
#include"stdio.h" int main() { int day = 0; switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break;\\To stop executing the following statement } }
Circular statement
First, let's look at the use of the while statement
#include"stdio.h" int main() { int i = 0; while(i<=10) { if (i == 5) break; printf("%d\n", i); } }
What numbers do you think will be output? 0, 1, 2, 3, 4,; Or 0, 1, 2, 3, 4, 5;
Is 0, 1, 2, 3, 4; Because break; Is a jump out of the following statement; Indicates that it is no longer executed; Break is used to permanently terminate a loop.
#include"stdio.h" int main() { int i = 0; while(i<=10) { if (i == 5) continue; printf("%d\n", i); i = i + 1; } }
How much do you think this will be? Before I tell you the answer, I'll tell you that continue is to stop this cycle. It can also be understood as this. It doesn't express 5. Is the output number 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10? The display is not, because it won't go down after 5, and it will always be in the dead cycle. After this change, it will change.
#include"stdio.h" int main() { int i = 0; while (i <= 10) { i = i + 1; if(i==5) continue; printf("%d\n", i); } }
This will not happen. When it reaches 5, it will stop the following cycle and jump to i=i+1; Note that continue is to stop this cycle, not to terminate the cycle.
for loop
for (initial; judgment; adjustment)
#include"stdio.h" int main() { for (int i = 1; i <= 10; i++) { printf("%d\n", i); } }//If you don't understand, you can debug it.
#include"stdio.h" int main() { for (int i = 1; i <= 10; i++) { if (i == 5) continue; printf("%d\n", i); } }
Let's look at some variants of the for loop
First kind
#include"stdio.h" int main() { int x, y; for (x = 0, y = 0; x < 2 && y < 5; ++x, y++) { printf("hehe\n"); } }
The second kind can be called dead cycle, which cannot jump out of the cycle because there are no judgment conditions,
#include"stdio.h" int main() { for (;;) { printf("hehe\n"); } }
Let's look at an interview question.
#include"stdio.h" int main() { int i = 0; int k = 0; for (i = 0, k = 0; k = 0; k++, i++) { k++; } }
They have no cycles.
do while loop
#include <stdio.h> int main() { int i = 10; do { if (i == 5) continue; printf("%d\n", i); } while (i < 10); return 0; }
Roughly the same as while