Select the C language structure and algorithm --RAPTOR if statement

Structure is selected according to a given determination condition, the determination result, and controls the program flow according to the result of the determination of
two, three cases selected configuration
selection structure is also referred to as a branched structure, can be divided into three cases
1. single branch selection structure (Single selection)
1) select faced: either to perform an operation, either skip it
a) good or bad decision to buy or not to buy based on Apple's quality
b) based on job performance, the unit decided to hire or not hire
2. dual branch selection structure (Double selection)
1) select faced: choose one of them in two different operations to perform
a) according to Apple's quality is good or bad to decide how much to buy: buy or buy less
b) according to your company performance, decided to give you how much fat bonus: more or less fat
3. multi-branch selection structure (multiple selection)
1) select faced: select two or more (continuous need to perform multiple conditional)
a) college entrance examination in batches admission, according to multiple choice candidates in order to fill in the admission
b) most wanted or most likely admissions schools and professional usually EDITORIAL Why?
The following flow chart
Here Insert Picture DescriptionHere Insert Picture DescriptionThird, select the structure instance
selection structure Example 1: The maximum number of two computers
1. Understanding the problem
2. Planning
Here Insert Picture Descriptionthe focus of our attention to, how to compare the size of a and b, the larger the number of stored max
1 With a single branch structure
Here Insert Picture Description2. The use of a double limb structure

Here Insert Picture DescriptionFour, RAPTOR Raptor structure and the selection of relational and logical operators
of our program right? That's triangle is arbitrary input values three sides, then these three values in the end can not be a triangle it? If that is not a triangle, we did find is definitely wrong, and only able to form a triangle we can use the formula to find the area of a triangle. Here it involves a branch structure is selected structure.
Branch structure (selection) Iconcore
1 may be determined by the condition, perform shunt
2. Do the same judgment condition statement True and False,
Here Insert Picture Description
as shown above, the structure of the conditional branch is typically a relational operators, and logical operators for selection and loop in conditions.
Here's relational operators is determined that the relational expression is established, such as in the establishment of expression evaluates to true, and false otherwise. Logical operators are and, or, not and is non. and (with) that several conditions are met jointly for the job is not met is false; or (or) is to meet a few conditions on the line, not only to meet is false, not (non) non-true or false , that is true nor false. About relational operators and logical operators do first understand here, in the course of next month, the operator will do a chapter explain in detail.
So we know these requirements triangle area flowchart should be changed to
Here Insert Picture Descriptionthe triangular sides of the triangle must meet any and should be greater than the third side and; therefore input three sides a, b, c, is first determined whether any two sides is greater than the third side when the sides are larger than any of the third side, is considered a triangle; or do not constitute a triangle. So conditions and should be a relationship, then our Raptor map should be amended as follows:

Here Insert Picture DescriptionAs shown above, the first input a, b, c trilateral after first determines whether enough triangular. First draw the selection structure, conditions in writing, because three conditions must be met simultaneously, so here is connected with and, before and after and to add a space.
When the condition is satisfied that form a triangle, it should take the yes branch is seeking output area, then you can select directly on the three boxes in FIG condition, the yes branch directly to the drag to the last output on the no branch can not form a triangle below
Here Insert Picture Descriptionexecution results as shown
1: If the input is not 1,2,3 triangular configuration, the output does not constitute a triangle.
2: If the input area is formed as a triangle, the output triangle is 3,4,5.
Fifth, the simple structures Raptor selection
using Raptor achieved Case: two input numbers, outputs a large value
known solution to the problem of a flowchart as follows: may be a single branch, the branch may be a double
Here Insert Picture Descriptionherein to achieve a method for Raptor the solution figure
as follows:
the first step: to draw a flow chart diagram Raptor: as
Here Insert Picture Descriptionthere will be a mistake: max can only appear on right side of assignment
mean: max only appears to the right assignment.
This is because the default max Raptor Lane is a function, so wrong, here you can just change a variable name, such as max1.
Here Insert Picture DescriptionSix, implemented by Raptor nested structure selected
solve the above problems, First we draw a flow chart: as
Here Insert Picture Descriptionshown above, a flow chart available max, Raptor Raptor FIG draw attention should be paid as follows:
Here Insert Picture Descriptionseven, three forms of the if statement
Select programming language syntax structure generally has two: First, if the statement; the second is the switch statement. First, we learn next if statement, if translated into Chinese is the meaning of if, now you can with Chinese "if" sentences, what would you think of it? If it --- --- --- otherwise.
1. Single branch statement IF
(1) format
if (expression)
statement;
if the expression is true (established), only the implementation of the statement; otherwise do nothing.
Note: the back if only one statement, you can not {}, {} must be added when more than one statement; it is recommended for beginners if () {} immediately after.
(2) the following code, the input 10 is greater than a number is displayed, no greater than 10 do not

/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
    int num;
    printf("请输入一个数:");
    scanf("%d",&num);
    if(num>10)
    printf("你输入的数比10大\n");
}

2. Dual-the else branch statement IF
(1) format
if (expression)
statement 1;
the else
statement 2;
if the expression is true (established), only the implementation of the statement 1; otherwise, the statement is executed 2.
Note: if the back of the statement 1 is only a statement that can not {}, when the plurality of statements must add {}; the else, and if the same, only a statement can not {}, when the plurality of statements must add {} ; it is recommended for beginners if () {} followed by followed by {} else followed.
(2) the following code, enter a number larger than 10 is displayed larger than 10, no greater than 10 outputs than 10

/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
    int num;
    printf("请输入一个数:");
    scanf("%d",&num);
    if(num>10)
printf("你输入的数比10大\n");
else
printf(你输入的数不比10大\n");
}

3. Multi-branch statement the else IF IF
(1) format
if (expression 1)
sentence 1;
the else if (expression 2)
sentence 2;
the else
statement 3;
if the expression 1 is true (established), then only execute the statement 1; otherwise the judge again the expression 2, 2 if the expression is true (established), then execute the statement 2, 3 or execute a statement.
Note: if the if-else if-else, as if only one statement when can not add {}, {} pieces must be added; after only one else statement can not add {}, {} pieces must be added.
(2) the following code, a number input, judgment is equal to or greater than 10 or less than 10 10.

/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
    int num;
    printf("请输入一个数:");
    scanf("%d",&num);
    if(num>10)
printf("你输入的数比10大\n");
else if(num==10)
printf("你输入的数等于10\n");
else
printf("你输入的数小于10\n");
}

4.if statement that compares three forms of
the if statement of the three forms, according to different conditions to determine, from the analysis and execution of the above code, in fact, we can also see if the single is actually a single branch, set up on the implementation of the establishment does not execution; and if-else we can see that it belongs to the two-branch, two roads, the establishment of a walk, do not walk another establishment, which is based on the condition choosing one go; and if-else if it is part of a multi-branch, can there are more than three roads, just go to judgment related expressions before every road, if left to set up appropriate way, it does not hold continue to determine the second expression, then judge, and so on, until it is determined to go that one (or both not established not go, there is no final else). So, take care of the writing program selection syntax, and you will learn very simple. Remember also critical statement format ah!
Flexible application 5.if statement
Comparison two code follows: Saying the difference between them; " inputs two numbers, sizes "

(1)/*单if实现*/
#include "stdio.h"
void main()
{
    int m,n;
    printf("请输入两个数:");
    scanf("%d%d",&m,&n);
    if(m>n)
    {
   		printf("第1个数大于第2个数\n");
    }
    if(n>m)
    {
   		printf("第2个数大于第1个数\n");
    }
    if(m==n)
    {
   		printf("第1个数等于第2个数\n");
    }
}
(2)/* if--else if实现 */
#include "stdio.h"
void main()
{
    int m,n;
    printf("请输入两个数:");
    scanf("%d%d",&m,&n);
    if(m>n)
    {
   		printf("第1个数大于第2个数\n");
    }
    else if(n>m)
    {
   		printf("第2个数大于第1个数\n");
    }
    else if(m==n)
    {
   		printf("第1个数等于第2个数\n");
    }
}

By the above described cases, if-else if-else if or can be converted into a plurality of single-if statement. However, when using a program or two-branch selection if-else, multi-branch election if-else if

Guess you like

Origin blog.csdn.net/SqrsCbrOnly1/article/details/91362607