# Basic algorithmic constructions

To perform typical sequences of actions in the algorithm, basic algorithmic structures have been developed in the form of a specific set of blocks and standard means of their connection. The basic algorithmic structures are linear, branching and cyclic.

A linear algorithm is an algorithm in which actions are carried out sequentially one after another.

An example of a linear algorithm is shown in fig. 10.5. Rice. 10.5. Linear algorithm

Unlike linear algorithms, in which commands are executed sequentially one after another, branching algorithms include a condition, depending on the fulfillment or non-fulfillment of which, one or another sequence of commands (actions) is performed. An example of a branching algorithm is shown in fig. 10.6.

A branching algorithm is an algorithm in which actions are performed along one of the possible branches of solving a problem, depending on the fulfillment of conditions.

As a condition in the branching algorithm, any statement understandable to the performer can be used, which can be met (be true) or not be observed (be false). Such a statement can be expressed both in words and in a formula. Thus, the branching algorithm consists of a condition and two sequences of instructions. Rice. 10.6. Branching Algorithm Rice. 10.7. Algorithm with a cyclic element

Multiple repetitions of the same actions are provided using cyclic algorithms (Fig. 10.7). The loop includes a condition check block and a block called the body of the loop as constituent elements. Before the beginning of the loop, the operations of assigning initial values to those objects that are used in the body of the loop are performed.

A cyclic algorithm is an algorithm in which some of the operations are performed repeatedly.

The word “repeatedly” in the definition of a cyclic algorithm does not mean “to infinity”. The organization of cycles that do not lead to a stop in the execution of the algorithm is a violation of the requirement of its effectiveness – obtaining a result in a finite number of steps.

If the loop body is located after the conditions are checked (a loop with a precondition), then under certain conditions the loop body will not be executed even once. This version of the organization of the loop, controlled by a precondition, is called a loop with a precondition (Fig. 10.8). Rice. 10.8. Loop with precondition

Another option for constructing a loop is that the body of the loop is executed at least once, and will be repeated until the condition becomes true. Such an organization of the cycle, when its body is located before checking the condition, is called a cycle with a postcondition (Fig. 10.9). The truth of the condition in this case is the condition for exiting the loop. Rice. 10.9. Loop with postcondition

Thus, the precondition loop terminates when the condition is false, and the postcondition loop terminates when the condition becomes true.

The third type of loops, in which the body of the loop is executed a given number of times (that is, before the loop starts, it is known exactly how many times it will be executed), is implemented using a counter. In a loop with a counter, the values of the counter in the body of the loop are incremented iteratively.

The process of solving a complex problem quite often comes down to solving several simpler problems using the method of sequential detailing. Accordingly, the process of developing a complex algorithm can be divided into stages of compiling separate algorithms, which are called auxiliary. Each such auxiliary algorithm describes the solution of some subproblem.

The process of constructing the algorithm by the method of sequential detailing is as follows. First, the algorithm is formulated in “generalizing” blocks, which may be incomprehensible to the performer and are recorded as calls to auxiliary algorithms. Then the detailing takes place, and all auxiliary algorithms are detailed to the level of commands understandable to the performer.