The general format of if-then-else statement is figure 1, and the corresponding program block diagram is figure 2.
Analysis: In the if-then-else statement, "condition" means the condition of judgment, and "statement 1" means the content of operation to be executed when the condition is met; "Statement 2" indicates the content of the operation to be performed when the conditions are not met; END IF indicates the end of the conditional statement. When the computer executes, the condition after IF is judged first, and if the condition is met, the statement after THEN is executed1; If the condition is not met, statement 2 is executed after ELSE.
3. Does the 3.IF—THEN statement meet the conditions? Whether the statement is (Figure 4) 4) The general format of the if-then statement is Figure 3, and the corresponding program block diagram is Figure 4.
Note: "condition" refers to the condition of judgment; "Statement" refers to the operation content to be executed when the conditions are met, and the program is terminated when the conditions are not met; END IF indicates the end of the conditional statement. When the computer executes, the condition if is judged first. IF the condition is met, the statement after the condition if is executed. If the condition is not met, the conditional statement is directly ended and other statements are executed.
Example 5: Write a program, input the coefficient of unary quadratic equation AX2+BX+C = 0, and output its real root.
Analysis: first, the idea of solving the problem is expressed in the program block diagram, and then according to the algorithm steps given in the program block diagram, the algorithm is gradually expressed in the corresponding program statements. (The program block diagram is discussed by the students first, and then unified. You can refer to the textbook. )
Algorithm analysis: before solving the equation, it is necessary to judge the sign of the discriminant, and then judge the root of the equation according to the sign of the discriminant: when △ > 0, the equation has two unequal real roots; When △ = 0, the equation has two equal real roots; When △ < 0, the equation has no real root. This process can be expressed by the conditional structure in the algorithm.
Summary of Exercise 2 of the textbook: Conditional statements are generally used in the algorithm design that needs to judge conditions, such as judging the positive and negative of a number, determining the size of two numbers, and finding the function value of piecewise functions. Conditional statements are often used, and sometimes even nested conditional statements are used.
General steps of programming: (1) Algorithm analysis: According to the provided problems, use the knowledge of mathematics and related disciplines to design an algorithm to solve the problems.
(2) Drawing program block diagram: Drawing program block diagram according to algorithm analysis. (3) Programming: According to the algorithm steps in the program block diagram, express the algorithm step by step with corresponding program statements.
The teaching of algorithm is not independent, it can penetrate into many compulsory contents, such as dichotomy, inequality solution, binary linear function and so on.