Preface: after talking about the basic knowledge, we will officially start the special explanation today. Subsequent bloggers will also update the special knowledge of functions, arrays, operators, pointers, structures and so on. Because the blogger is also a Xiaobai who has just come into contact with the C language, it is inevitable that there will be some deficiencies in the articles, but I will write these topics carefully, and we will officially start learning without much talk.
1. Branch statement if
If is a branch statement, which can also be called a conditional statement. There are four ways to write an if branch statement. Let's study it together here
1. The first way of writing
The first way to write if is:
A brace {} here is called a branch. If can be translated into if, so it can also be called a conditional statement. When the expression is true, the statement in the brace will be executed, and when the expression is false, the if statement will not be executed. Of course, the number of statements in the brace can be random and determined according to your own needs. Note: in C language, 0 means "false", and all non-zero values are "true".
Example:
#include <stdio.h> int main() { int i = 0; //Enter a value for i scanf("%d", &i); if (i < 10) { printf("%d\n", i); } return 0; }
Here, we enter a number to judge whether i is less than 10. If it is less than 10, it is true, and the entered value i is printed. If it is greater than 10, it is false, and the statement in if is not executed.
2. The second way of writing
Else stands for other. When the expression is true, execute the statements in branch 1. When the expression is false, execute the statements in else. Note: when using this writing method, there must be a branch to execute.
Example:
#include <stdio.h> int main() { int i = 0; //Enter a value for i scanf("%d", &i); if (i < 10) { printf("i = %d\n", i); } else { printf("Hello\n"); } return 0; }
When the condition is true, it will end after printing i:
When the condition is not true, print Hello and end:
3. The third way of writing
This way of writing is to judge expression 1 first. If it is true, branch 1 will be executed, and the whole if statement will end after execution. If expression 1 is false, judge expression 2. If it is true, branch 2 will be executed, and the whole if statement will end after execution. And so on, that is, as long as one branch is executed, the if statement ends. If all expressions are false, the entire if statement will not be executed.
Example:
#include <stdio.h> int main() { int i = 0; //Enter a value for i scanf("%d", &i); if (i < 10) //Branch 1 { printf("haha\n"); } else if (i > 12)//Branch 2 { printf("yyds\n"); } else if (i == 10)//Branch 3 { printf("Hello\n"); } return 0; }
When the condition of branch 1 is true, the statement ends after printing haha:
When branch 1 is false and branch 2 is true, the statement ends after printing yyds:
When branch 1 and 2 are false and branch 3 is true, the statement ends after Hello is printed:
Do not execute when branches 1, 2 and 3 are false:
4. The fourth way of writing
This writing method can be inferred from the above three writing methods. When branch 3 and the previous branches are false, else is executed. When using this method, there must be a branch. Here can be launched through the above three examples, so bloggers will not give examples.
2. Branch statement switch
1. Usage
Note: when using the switch statement, the floating point number cannot be used in the bracket of switch(), otherwise an error will be reported. The indentation of the case statement is the same as that of switch
Here we explain the code:
#include <stdio.h> int main() { int day = 0; //We treat day as a week and enter the day of the week. scanf("%d", &day); 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; default: printf("The value entered is not in the range,"); } return 0; }
Usage: the value in the expression in switch (expression) matches the value of case. If it is the same, execute this case statement, that is, the value of day matches the value after case.
Here, we enter 2 to execute the statement of case 2: that is, print Tuesday.
Enter 6 to print Saturday:
The function of default statement is to execute the statement in default when the value you enter does not match the value of case. Of course, in the switch branch statement, the default statement is writable or not.
2.case penetration
Of course, the break in the case statement can be omitted. When omitted, case penetration will occur, for example:
#include <stdio.h> int main() { int day = 0; //We treat day as a week and enter the day of the week. scanf("%d", &day); switch (day) { case 1: printf("Monday\n"); case 2: printf("Tuesday\n"); case 3: printf("Wednesday\n"); case 4: printf("Thursday\n"); case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("The value entered is not in the range,"); } return 0; }
Execution results:
If 2 matches the case, Tuesday will be entered. If there is no break, it will continue to execute. Find the break and stop after finding it.
Of course, nothing can be written in the case statement:
#include <stdio.h> int main() { int day = 0; //We treat day as a week and enter the day of the week. scanf("%d", &day); switch (day) { case 1: case 2: case 3: case 4: case 5: printf("go to school\n"); case 6: case 7: printf("Play games\n"); } return 0; }
Here 1 2 3 4 5 are all school, while 6 7 are playing games:
3. Use character type
#include <stdio.h> int main() { char a = '0'; //Enter characters for a scanf("%c", &a); switch (a) { case 'A': printf("sleep"); break; case 'B': printf("study"); break; case 'C': printf("having dinner"); } return 0; }2, Circular statement
1. For loop
First: the initialization expression is executed first and only once in the whole loop.
Second: the result of conditional expression must be true or false, that is, 0 and non-zero expressions.
Execution principle:
1. Execute the initialization expression first, and the initialization expression is executed only once.
2. Then judge the result of the conditional expression. If the result of the conditional expression is true, execute the loop body.
3. After the loop body ends, execute the update expression.
4. After updating, judge the result of the conditional expression. If it is true, continue to execute the loop body, and so on until the for loop terminates when the conditional expression is false
Here we can see the figure. After executing the order of 1 ~ 5, if the conditional expression is true, continue to execute (see the gray line here) until the conditional expression is false and the for loop ends.
For example, we want to print 1 ~ 10:
#include <stdio.h> int main() { int i = 0; for (i = 1; i <= 10; i++) { printf("%d ", i); } return 0; }
Execute i = 1 for the first time, and then execute the judgment statement to judge i < = 10. If it is true, execute the statements in the loop body. After executing the loop body, i + +. At this time, i = 2, and then execute the judgment statement i < = 10. If it is true, execute the statements in the loop body... And so on until i = 10 after the tenth loop. After executing i + +, i = 11, and then execute i < = 10, At this point, it can be seen that the condition is not immediately false, so the for loop terminates.
Of course, the initialization expression and update expression of the for loop can not be written, while the conditional expression generally needs to be written. If it is not written, it will be determined to be "true" and enter the dead loop, whether you omit the expression or not
Semicolons (;) cannot be omitted.
#include <stdio.h> int main() { int i = 1; for (; i <= 10; ) { printf("%d ", i); i++; } return 0; }
The for loop can also enter multiple conditions, as shown in the figure:
2. While loop
Execution principle:
1. First judge whether the conditional expression is true or false. If it is true, execute the loop statement, and then judge the conditional statement... Until the conditional expression is false, while the loop terminates.
#include <stdio.h> int main() { int i = 1; while (i <= 10) { printf("%d ",i); i++; } return 0; }
The effect of this writing is the same as that of the for loop, and it is also very similar. You can simulate the execution sequence and experience it. The results are:
3. Do while loop
Execution principle:
1. The execution principle of do while is actually very similar to that of while. Come in and directly execute the statements in the loop body, and then judge the conditional expression. Note that a semicolon needs to be added after while(). Using the do while loop must execute the loop body once.
I. continue
Continue means continue. It is used for for, while, do while loops to terminate this loop and enter the next loop to continue execution.
1. For the for loop:
int main() { int i = 0; for (i = 0; i <= 3; i++) { if (i == 2) { continue; } printf("%d\n", i); } }
Why not print 0 1 2 3 here, because when i = 2 for the third cycle, the if conditional statement is judged as true, execute continue, so that it does not execute the following statement and directly proceed to the next cycle. At this time, i + +, judge again.
2. Used for while loop
#include <stdio.h> int main() { int i = 1; while (i <= 5) { if (i == 2) { continue; } printf("%d\n", i); i++; } }
result:
We can see that the cursor is flashing, which means an endless loop. This is because when i = 2 enters the loop for the second time, the judgment of the if statement is true. Enter the branch and execute continue to make it carry out the next loop without executing the following number. If I is not changed, it will always enter the loop with the value of 2 and then execute the if statement, resulting in an endless loop. To prevent dead loops, we can put i + + on if.
3. For do while loop
#include <stdio.h> int main() { int i = 0; do { printf("haha"); continue; i++; } while (i < 3); }
Here we can practice by ourselves and see what the results will be.
II. break
1. Used for switch statements to prevent case penetration.
2. Used for for, while, do while loops to terminate the execution of the loop.
What happens when we change the continue above to break
#include <stdio.h> int main() { int i = 0; for (i = 0; i <= 3; i++) { if (i == 2) { break; } printf("%d\n", i); } }
Print 0 and 1. When entering the loop body for the third time, i = 2. If the if conditional statement is met, execute break and the for loop ends directly.
There are no traps when the break is applied to while, do while, which is the same as for. When a break is encountered, the loop is ended directly.
4, getchar and putcharThe return value of getchar is of type int.
EOF is returned when gatchar is not a character.
As you can see below, getchar is used to receive a character and putchar is used to output the character. When it is not equal to EOF, it will be printed all the time. That is, when it is not a character, it will be equal to EOF. int a = 0 is used because getchar returns int type
Next, let's simulate the scenario of user login
#include <stdio.h> int main() { char password[20] = { 0 }; printf("Please input a password:"); //Input password scanf("%s", password); printf("Please confirm the password(Y/N): "); int ch = getchar();//Enter Y/N into ch if (ch == 'Y') { printf("Confirm success\n"); } else { printf("Confirm failure\n"); } return 0; }
In principle, after we enter the password, we can enter Y/N to determine the password. But why give a definite failure without losing Y/N. The reason is that when we use the input function, we will put the buffer first. When we use the scanf function to input 123456 on the keyboard, we need to press the Enter key to determine the number of inputs, that is, \ n, so there will be 123456 in the buffer. \ n
At this time, we just need to use getchar to receive \ n.
#include <stdio.h> int main() { char password[20] = { 0 }; printf("Please input a password:"); //Input password scanf("%s", password); printf("Please confirm the password(Y/N): "); getchar();//Take away \ n int ch = getchar();//Enter Y/N into ch if (ch == 'Y') { printf("Confirm success\n"); } else { printf("Confirm failure\n"); } return 0; }
Here we can see that we can make a normal judgment, but the problem comes again. What if the user enters like this:
When we use scanf to receive 123456, when we use spaces, the front of the spaces indicates that 123456 is input to a variable, that is, the variable ch. at this time, there is still "space" abcd\n left in the buffer, while using a getchar only takes away the spaces and there is still abcd\n left behind, so we need to use a loop to receive.
#include <stdio.h> int main() { char password[20] = { 0 }; printf("Please input a password:"); //Input password scanf("%s", password); printf("Please confirm the password(Y/N): "); int teap = 0; while ((teap = getchar()) != '\n')//Used to receive redundant values { ;//Empty statement } int ch = getchar();//Enter Y/N if (ch == 'Y') { printf("Confirm success\n"); } else { printf("Confirm failure\n"); } return 0; }
while ((teap = getchar()) != '\n') { ;//Empty statement }
This means that the character returned by getchar() is assigned to the variable teap. When it receives \ n, the execution conditions of the while loop are not met, and the values of the buffer are taken out. Therefore, the next getchar() cursor will stop and wait for the input character
This time, let's look at the results:
Then we can see success. Here we can understand first and then type out the complete code ourselves.
Let's do some exercises to consolidate our knowledge:
Little practice1
Well, branches and loops, that's all we've learned. If it's helpful to you, remember to collect it. Those who like this chapter can also use their small hands to praise it. If you find any mistakes in the article, please correct them in time. Thank you.