One python3 Programming Fundamentals: Program Structure

  Program from the program enters the inlet, the program execution ends, is generally in the order of statement execution, the function or code block structure, control structure of a program, the program to help grasp the body frame.

1, sequential structure - the most common structure of
  the program sequence structure is designed to be the easiest, just write the corresponding statement in order to solve the problem on the line, it's execution order is from top to bottom, in sequence. Execution of the program in strict accordance with the order of program statements appear in the program execution, if you modify the program statement of the order, it will affect the results of the implementation of the program. In fact, no matter how complex the program, it is probably in the order of execution of the structure.

  

  

  Sequence structure can be used independently to form a complete simple procedure, the common input, here the statement A, is calculated, where the statement B, C output statement herein trilogy is the order of the program structure. For example, to calculate the area of ​​a circle, the order of arrangement, the need to enter the circle radius r, according to the formula s = pi × r × r calculated area of ​​a circle; s final output area of ​​a circle, the program ends.

However, most cases are sequential structure as part of the framework, often constitutes a complex process with other structures, e.g. compound statement branching structure, the cyclic structure like loop. Although the program order to solve the structure calculation, the output and so on, but can not make a judgment reselection. For determination should first issue reselection is necessary to use a branched structure.

2, the branch structure - the most flexible program structure

  Execution of the branch structure is select an execution path based on certain conditions, not the physical order in strict accordance with the statement appearing. The key programming method is to construct a branch structure and the appropriate branch condition analysis program flow, select the appropriate branch statement depending on the program flow. Some statements in accordance with the data in case the program is running, jump to a different branch execute different statements. Branch structure suitable for comparison with a logical OR relationship determination calculation conditions, the design of such procedures tend to be drawn to the program flow chart, and then write process according to a program source, this analysis and to separate programming language, make things simple and easy to understand. The program flow chart is a flowchart of a program executed solving analysis drawn.

3, circular structure - the most persevering structure

  Loop structure can reduce the workload of writing source code repeat, repeat the question used to describe a certain algorithm, which is a program structure programming computers to play the most expertise, C language provides four cycles, namely the while loop, do -while loops, for loops and goto cycle. Four kinds of cycles can be used to deal with the same issues, they can be replaced by another change under normal circumstances, but generally do not promote the use of goto loop because often forced to change the order of the program brings unexpected errors will run the program, we are learning The main learning while, do ... while, for three loop.

  Key three loop structure commonly used to ascertain the learning they are the same except for use at different occasions, it is necessary to perform a clear sequence and format of the three loop, each loop of the flowchart after a thorough understanding You will understand how to replace use.

  As the while loop example, by rewriting a program for statement, so a better understanding of their role. Particular attention should be included in the body of the loop toward the end of the statement (that is, changing the value of the loop variable), otherwise it could become an endless loop, which is a common beginner's mistake. Please note that conditions have changed the order of code execution issue.

  

4, the modular program structure

A modern programming language, modular structure with function to achieve, is about a complex program is divided into several modules, each module are written into a function or package, and then call the function calls the function main function is implemented by a large-scale problem Programming. Algorithms + data structures = programs. Wherein the number of completed structural formula defined function of the algorithm is mainly implemented by a function, the function call is completed to test the function, the return value and the like. We need to focus on understanding and application programming and debugging to be consolidated through the machine.

Guess you like

Origin www.cnblogs.com/guochaoxxl/p/11785902.html