It has been proved theoretically that all computersBeijing website constructionThe program can be described by three basic control structures: sequence, selection and cycle.
1. Sequence structure
The sequence structure is used to represent a sequence of calculation operations that require sequential processing.The calculation process starts from the first operation described to the last operation of the sequence, and is executed sequentially, as shown in Figure 7-3.The sequence structure can also contain other control structures.
2. Select Structure
The selection structure represents the logical structure for selecting one of two or more processing branches. The basic selection structure is to specify a condition P, and then decide whether to execute calculation A or B according to whether the condition is true, that is, select one of the two branches to execute, as shown in Figure 7-4 (a). Calculation A or calculation B in the selection structure can also include orderSelect and cycle structure.The program language usually also provides a simplified selection structure, that is, a branch structure without calculating B, as shown in Figure 7-4 (b), and a multi branch selection structure.
3. Cycle structure
The loop structure describes the process of repeated calculation. It usually consists of three parts: initialization part, the part that needs repeated calculation (called loop body), and the condition part for repeated calculation. The initialization part is sometimes not explicitly expressed.There are mainly two types of loop structure: while type loop structure and do=while type loop structure.
The logic meaning of while type structure is to judge condition P first, if it is true, execute loop body A, and then judge condition P, otherwise the control flow will exit the loop structure, as shown in Figure 7-5 (a).
The logical meaning of the do while structure is to execute the loop body A first, and then judge condition P. If it is true, continue to execute the loop body A, and then judge condition P. Otherwise, the control flow will exit the loop structure, as shown in Figure 7-5 (b).An example of the calculation process represented by sequence, selection and cyclic structure is shown in Figure 7-6.Wherein, the calculation process shown in Figure 7-6 (a) means that shilling x obtains a value of 5, then y obtains a value of 7, finally the values of x and y are added and the results are stored in two;The calculation process shown in Figure 7-6 (b) means that first judge whether the value of x is greater than y, if so, let max obtain the value of r, otherwise let ma x obtain the value of y;The calculation process shown in Figure 7-6 (c) shows that shilling i obtains a value of 0, then judges whether the value of i is less than 10, if so, increases the value of i by 1, then judges the value of i to determine whether to continue to increase, and repeats the process until the value of i is equal to 10.