Circulating structure

Structure Introduction

The loop structure can be seen as a conditional judgment statement and a combination of an directional direction statement. In addition, three elements of the circulating structure: circulating variables, cyclicers, and cyclic termination conditions. The loop structure is used in the block diagram to use the judgment box, and the criterion is written in the box, and the two outlets correspond to the conditional establishment and condition, respectively. Different instructions performed when not set up, one of which is to point to the cyclic body, then return from the cyclic body to the entrance to the judgment box.

C language circular statement

three Four cycles are available in the cycle

C language, namely the Goto loop, while loop, DO ... While loop and for loop. Four cycles can be used to handle the same problem, in general, they can replace each other, but generally do not advocate the GOTO cycle, because the order of forcing the program will often give the procedure to run unpredictable errors, in learning us Mainly learn the three cycles of While, Do ... while, for. Commonly used three kinds of loop structural learning focus is to clarify their same and different from them to use them in different occasions. This will clearly understand the format and execution order of the three cycles. After understanding the flow chart of each loop, you will understand how to replace it, such as the example of the WHILE loop, re-writing with the for statement, which can better Understand their role. In particular, it should be noted that the statement that tends to end in the cyclic body (that is, the cyclic variable change is changed), otherwise it may become a dead loop, which is a common mistake of beginners.

Three cycle differences

After learning these three cycles, it should clarify their difference: When using the While and DO ... While cycle, the initialization of the circular variable should be Before the cyclic body, the FOR cycle is generally carried out in the statement 1; the While cycle and the for loop are first judged, and then the cyclic body is executed; and the DO ... While loop is to determine the expression after the cyclic body is executed, that is Said that do ... while the cyclic body is executed once, while the While loop and for do may not be executed once. It is also important to note that these three cycles can be jumped out of the cycle with the BREAK statement, and the CONTINUE statement ends this cycle, and the loop of the GOTO statement with the IF is not controlled with BREAK and CONTINUE statements.

sequential structure, branch structure and circulation structure do not isolate each other, can have branches, sequential structures, and branches in the cycle, and in the branch, there is no matter which structure, we can It is broadly to see them as a statement. These three structures are often combined in the actual programming process to achieve various algorithms, and the corresponding procedures are designed. However, the problem to be programmed is large, the program written is often very long, the structure is more replenishing, causing readability, difficult to understand, the method of solving this problem is to design the C program into modular structure.

Modular program structure C language modular program structure is implemented, the complex C program is divided into several modules, each module is written into a C function, then pass the main function call function and The function call function is written in a C program of a large problem. Therefore, it is often said: C program = main function + subunies. Therefore, the definition of the function, the return, the return, and the value of the value should be particularly paying attention to understanding and application, and consolidated by pressing the machine.

When the condition is established, the code of the cyclic body is executed. When the condition is not established, the loop is jumped out, and the code behind the loop structure is executed. The loop structure can reduce the workload of the source program repeated written, which is used to describe the problem of repeating a certain algorithm, which is the program structure that best exerts computer specials in programming. The loop structure can be regarded as a conditional judgment statement and a combination of a direction of rotation. In addition, three elements of the circulating structure: circulating variables, cyclicers, and cyclic termination conditions. The loop structure is used in the block diagram, which is used to use the judgment box. The determination box is written on the box, and the two outlets correspond to different instructions performed when the condition is established and the condition is not established. One is to point to the cycler, and then then from the cyclic body Back to the entrance to the judgment box.

Common two types of cyclic structures

1 When the type of loop is determined whether the condition P is established, if P is established, then execute A (step ); If the condition P is established; if P is established, then A, if this is repeated, until a certain condition P does not appear.

2 Until the loop: first execute A, then determine if the condition P is established, if P is not established, then execute A, so repeated until P is established, the cycle The process ends.

Related Articles
TOP