Example explains the cyclic structure of the C language

zhaozj2021-02-17  75

Before this lecture begins, we need to prepare a function. The role of this function is to convert a percentage score to a grade system. The process of conversion I have explained in detail when I explain the branch structure, and now I only provide functions as follows:

/ * Prepare grade.h * / char grade (int score) {switch (score / 10) {case 10: case 9: return 'a'; case 8: return 'b'; case 7: return 'c'; Case 6: Return 'D'; Case 5: Case 4: Case 3: Case 2: Case 1: Case 0: Return 'e'; default: return 0;}}

The instances raised when explaining the branch structure can only solve a result. If you need to convert 5 people's scores, the easiest way is as follows:

/ * Example 1 * / #include "grade.h" #include void main () {int N; printf ("Please enter grade:"); scanf ("% d", & n); printf ("Level:% C ", grade (n)); Printf (" Please enter a score: "); scanf ("% d ", & n); printf (" Level:% C ", grade (n)); printf (" Please enter Results: "); Scanf ("% D ", & n); Printf (" Level:% C ", grade (n)); Printf (" Please enter grade: "); scanf ("% d ", & n) PRINTF ("Level:% C", grade (n)); Printf ("Please enter a score:"); scanf ("% d", & n); printf ("Level:% C", grade (N ));

Example 1 The structure is very easy to understand, but it is only a feeling of writing. The same three sentences, actually repeated 5 times. If the number of people in this class is more than 5, but dozens of people, ... days! I don't want to think about it!

The C language certainly doesn't be stupid. It is convenient to give it. This "convenient" is a loop structure. The purpose of the loop structure is to reduce duplicate code and reduce the burden of programmers. In its form, there are three types in the C language: for loops, while cycles and do-while loops. The front is described as follows:

The basic format of the FOR cycle is for (statement 1; logical expression 1; statement 2) {statement group 1}, braces include the statement group 1 in which it can be a statement. The implementation process is as follows: First execute the statement 1, and then determine the logical expression 1. If the value of the logical expression 1 is "true (non-0)", the statement group 1 is executed, otherwise the loop is ended. After the cycle is not ended, the statement group 1 is executed, and the statement 2 is executed. Then, the logical expression 1 is then judged, then ..., so that the logical expression 1 is "false (0)". Thus, Example 1 can be rewritten as follows:

/ * Example 2 * / #include "grade.h" #include void main () {INT N, I; for (i = 0; i <5; i ) {printf ("Please enter grade:"); scanf "% d", & n); Printf ("Level:% C", grade (n));}} Hey, is it so simple? ! I know the convenience of the loop structure. The i from 0 to 4 in Example 2, a total of 5 cycles were performed. Of course, it is also possible to rewrite the FOR statement in Example 2 as follows by personal habits.

FOR (i = 1; i <= 5; i ) {...}

This may be more likely to understand, but I don't recommend this because the array subscript in the C language is starting with 0, and the for statement in Example 1 is easier to read the array element in the cyclic body. In the case, if we want to print it again after all the results are completed, you need to use the array:

/ * Example 3 * / #include "grade.h" #include

#define n 5

Void main () {Int n [n], i; for (i = 0; i

In Example 3, if I cycle from 1 to N, N [i - 1] instead of N [i] should be used when the array element is read from 1 to N. Then, every time the cycle is executed, the subtraction operation will be subtracted. In a loop N times cycle, the N times subtraction operation will greatly reduce the efficiency of the program efficiency.

C language is a flexible language. Its FOR cycle is not a constant, and must be written in its basic format. Temporarily sell a Cat, first look at the example 4 from Example 3 Changed 4:

/ * Example 4 * / #include "grade.h" #include

#define n 5

Void main () {int N [n], i = 0; for (; i

Is it very strange that two for cycles in Example 4 have some statements, how is it more than the FOR cycle of the above two examples? And listen to me slowly:

In the first for cycle, less I = 0 and i two sentences. In fact, look at it carefully, this two sentences are not less. i = 0 has been implemented when it is assigned to i, and i is implemented in a SCNAF statement. So, although there is no I = 0, the value of i before the start of the cycle is already 0; although there is no i , the value is also changed in the cyclic body. There is only one sentence that is just that sentence in the second for cycle. It is because of the less than this sentence, the FOR cycle cannot be ended. If I don't take measures in the cyclic body, this loop will continue to perform the dead cycle. Thus, I judge i in the cyclic body, execute a BREAK statement when it is equal to N, and jumps out of the loop, fill the defects of the log statement in the FOR statement.

Even, a FOR statement can be written into for (;;) {...}, such a for statement, simply, is a dead cycle.

There is also something to explain, that is, the Break statement. This statement has been seen when explaining the Switch branch structure. In addition to the Switch branches, it can also be used for all three cyclic structures. Its role is two words - "jump out". In the SWITCH branch structure, it is used to jump out of the entire SWITCH statement; in the loop structure, it is naturally used to skip the loop. After performing it, all other statements in the cyclic body will not be executed, and the entire loop is interrupted.

Learn for the for loop, although it can simplify a lot of code, it seems to solve the case where the cycle is fixed. What should I do if the number is not fixed? For example, the following: Enter a grade of several 0 to 100, if the score is not here, the expression ends. Please see Example 5, Example 6:

/ * Example 5 * / #include "grade.h" #include void main () {int N; printf ("Please enter grade:"); scanf ("% d", & n); while (grade (n)! = 0) {Printf ("Level:% C", grade (n)); Printf ("Please enter the grade:"); scanf ("% d", & n);}}

/ * Example 6 * / #include "grade.h" #include void main () {int N; do {printf ("Please enter grade:"); scanf ("% d", & n); if (grade) )! = 0) {Printf ("Level:% C", grade (n));}} while (grade (n)! = 0);}

Examples 5 and Example 6 use the While cycle and the Do-While loop structure, respectively. Both cyclic structures are logical expressions after whiling to perform cyclicers, and end cycles for the time. The difference between the two is that the While loop is first judged, then performs the cyclic body; and the Do-While loop is before the cyclic body is executed. Therefore, at least one cycle body is to be performed using a Do-While cycle. As for the execution process of the loop, please analyze himself.

In Example 6 For the same N, at least two grade (n) is to be executed, greatly reduced the efficiency of the program. Although this problem can be solved with a CHAR type variable, I don't want to make this. Therefore, there is another way of rewriting: / * Example 7 * / #include "grade.h" #include void main () {int N; do {printf ("Please enter the grade:"); scanf ("% d", & n); IF (grade (n) == 0) {Break;} Printf ("Level:% C", grade (n));} while (1);}

The logical expression of the WHILE statement in Example 7 is always true (1), so this loop is a dead cycle, and must be jumped out in the cyclic statement. This is also very well understood with an IF statement to jump out, which is very well understood. But look at the example 8 below, it may not understand so much.

/ * Example 8 * / void main () {int N; do {printf ("Please enter grade:"); scanf ("% d", & n); if (grade (n) == 0) {Continue;} Printf ("Level:% C", grade (n));} while (grade (n)! = 0);}

The difference between Example 6 is mainly in a CONTINUE statement. This statement is unique to the loop structure, and its role is similar to the Break statement, which is similar to the relative. After executing the Continue statement, the statement below the cyclic body will no longer be executed in this loop, but the loop is not interrupted, but the next loop is started.

As in Example 8, the CONTINUE statement was executed after grade (n) == 0. Then the PrintF statement is skipped, directly to the While statement to determine if the loop is ending, and if the condition of the end cycle is not met, then the next grade will be required. However, in Example 8, as long as the CONTILE statement can be performed, the conditions after while must be false, and the loop ends. The difference between Example 8 and Example 7 is that Example 8 is normal end, and Example 7 is interrupted.

Break and Continue Two statements have a pivotable role in the C language loop structure. Although the use of smart structured procedures can avoid using these two statements, these two statements do make program structures more simple. So, as long as I can use them, I will try to use them.

The three loop structures are finished, but I have to emphasize the flexibility of the C language. Although the circulating structure has three forms, only one of them is still available. Examples 5 and Example 6 showed the tasks of completing the same thing in both while and do-whilele. As for the for loop, it is said that it can only solve the fixed number of cycles "," this seems "two words are not pen. It looks at the situation where the number of cycles can be resolved, but it is clear that it can solve the case where the number of cycles is not fixed. In the case of the example, the form of the inventory can be rewritten as:

/ * Example 9 * / #include "grade.h" #include void main () {int N; printf ("Please enter grade:"); scanf ("% d", & n); for (; grade (n) ! = 0;) {Printf ("Level:% C", grade (N)); Printf ("Please enter a score:"); scanf ("% d", & n);}} / * case 10 * / #include "grade.h" #include void main () {int N; int b = 1; for (; b;) {printf ("Please enter grade:"); scanf ("% d", & n); if (Grade (n)! = 0) {Printf ("Level:% C", grade (n));} else {b = 0;}}}

Take a look at Examples 9 and Examples 5, Examples 10 and Example 6. Is there a wonderful work? In fact, the circular structure of the C language is far more than these deformations. As long as you think care, you will definitely find more "deformed". Hahahaha, a spit is fast, and it is much more comfortable. = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -

//

__________ / lb / ___ Outinn

/ _ [] _ ​​/ ____ / /

/ _________ / | () | / __ /

Http://outinn.yeah.net/

| ____ / - | __ | - / | Welcome to Visit Outinn!

| __ | == | ___ | || | __ |

- = - = - = - | _ || _ | = - FANCY,

Outinn@china.com

转载请注明原文地址:https://www.9cbs.com/read-31333.html

New Post(0)