Introduction to program structure and branch statements

Source: Internet
Author: User

    1. Structure and classification of the program
    2. Introduction to Branch statements
      1. Introduction to the IF statement
      2. The switch statement describes
    3. Introduction to Circular statements

Structure and classification of the program

The structure of the program is a design idea, a set of methods, is the program has a reasonable structure to ensure and verify the correctness of the program. This method requires the programmer, Boone, to write the program as he pleases, and to design and write the program according to a certain structure. One of his important aims is to make the program have a good structure, make the program easy to design, easy to understand, easy to debug and modify to improve the efficiency of the design and maintenance program work.

  

The three basic structures of structured programming are: sequential structure, selection structure (branching structure), and cyclic structure.

The sequential structure represents that the actions in the program are performed in the order in which they appear.

The process step for selecting a struct represents a branch, which requires that one of the branches be selected according to a particular condition. The choice structure has the single choice, the double choice and the multi-choice three kinds of forms.

A looping structure indicates that a program performs one or more operations repeatedly until a condition is false (or true) to terminate the loop. The main thing in the loop structure is: Under what circumstances does the loop execute? What operations need to be executed in a loop? There are two basic forms of cyclic structure: the type loop and the until type loop.

When the type of cycle: the first to determine the conditions, when the given conditions to execute the loop body, and at the loop terminal process automatically return to the loop inlet; If the condition is not satisfied, exit the loop body directly to the process exit.  Because it is "executing the loop when the condition is satisfied", that is, it is executed after judgment, so it is called a type loop. Until the type of loop: from the structure of the entrance to execute the loop body, at the end of the loop to determine the condition, if the condition is not satisfied, return to the entrance to continue to carry out the loop body, until the condition is true and then exit the loop to reach the exit of the process, is first executed after judgment Because it is "until the condition is true", it is called until the type loop. Introduction to Branch statements

The program of sequential structure can solve the problems of calculation and output, but can not make judgment and choose. The branch structure is used for questions that need to be judged before they are chosen. The execution of a branch structure chooses the execution path according to certain conditions, rather than the physical order in which the statement appears. The key of the program design method of branch structure is to construct the proper branch condition and the analysis program flow, and select the appropriate branch statement according to the different program flow. The branch structure is suitable for the calculation of the conditional judgment with logical or relational comparison, and the design of this kind of program often has to draw its program flowchart, and then write out the source program according to the program flow, which separates the programming analysis from the language, makes the problem simple and easy to understand.

The branching structure in the C language includes: If statements and switch statements.

Introduction to the IF statement

Form one (IF): If the expression is true, the statement block 1 is executed, otherwise it is not executed.

1 if (expression) 2 {3     statement block 1;      4}5     subsequent statements;

Form two (If...else ...) : EXECUTE statement block 1 If expression is true, or EXECUTE statement block 2

1 if (expression) {2     statement block   1;3 }else{4     statement Block 2 ;5 }

Form three (If...else if...else if ... else): There can be more than if and else if, only the code block content corresponding to the first true expression is executed from the top down.

From the beginning of expression 1, when the expression is true, execute the statement block corresponding to the expression, for example, from Expression 1 to judge until the expression n is the value of the expression is true, then executes the contents of the statement block N, when the statement block execution ends out of the entire if statement. If the value of expression 1 to expression N is false then the statement block n+1 after else is executed.

You can omit the last else and statement block content as needed.

1 if(expression 1) {2 statement block 1;3}Else if(expression 2) {4 statement Block 2; 5}Else if(expression 3) {6 statement block 3; 7 }8     ...9 Else if(expression N) {Ten statement block N;  One}Else{ AStatement Block n+1;  -}

Form four (if statement nesting): If statement can also nest if required.

Where to use the IF statement is worth noting:

#1 if (expression);//if followed by an empty statement, which means that if the value of an expression is true, nothing is done.

#2 if (expression) return 0;//The function will not execute down and exit directly. A function can have multiple return, but only one works.

#3 if (not 0 constant) means that the eternal, if (0) represents a perpetual false. No real sense.

The #4 encounters an if statement that omits curly braces, and then matches the else match if

The switch statement describes

Introduction to program structure and branch statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.