Basic types and example of cyclic algorithms

The article is intended to give basic concepts about what a cyclic algorithm is, which are common to any programming language and the level of training of a programmer.

Concept of algorithm

An algorithm is a sequence of actions to achieve a solution to a computational and other problem in a finite number of steps. Actions (instructions) for the execution of the algorithm can be performed one after another (sequentially), simultaneously (in parallel) or in an arbitrary order, using cycles and transition conditions. Algorithms are used not only in programming, but also in other fields of activity, for example, in the management of production and business processes.

Loop Algorithms

An algorithm is called cyclic if it contains actions or sets of actions that need to be performed more than once. Repeated algorithmic actions are the body of the cycle. Additionally, each cycle has a condition by which the execution of the cyclic algorithm ends.

Types of cyclic algorithms

Each cyclic algorithm incorporates a loop condition, i.e., a logical expression, the verification result of which determines whether the body of the loop will be executed again or the loop will be completed. According to the processing method, all cyclic algorithms are divided into three groups.

Preconditioned Cycle

In such cyclic algorithms, the continuation condition is checked before processing the body of the cycle, i.e., there is a need to repeat the processing of the cycle.

Consider printing numbers from -5 to 0 as an example of cyclic algorithms with a precondition:

example of cyclic algorithms
Elements of the algorithm:

  1. Set the initial value of the base variable j equal to -5.
  2. Check the condition of the loop. The condition is positive, and the loop body is executed for the first time.
  3. Next, add one to the variable j, again check the loop condition.
  4. The loop continues to run until the value of j is less than zero or equal to it, otherwise we exit the loop on the FALSE branch

Postcondition Loop

Checking the condition is performed after the first processing of the cycle body and controls the exit from it.

Let us examine the calculation of the sum from 1 to the number n as an example of cyclic algorithms that use the postcondition:

an algorithm is called cyclic if

  1. We introduce the final number for calculating the sum n and set the zero initial values ​​of the total sum and cycle counter i.
  2. The cycle is executed until the first condition check.
  3. We check the condition of the cycle, i.e., the value of counter i is less than or equal to n.
  4. If the result of the condition is positive, we run the cycle again, otherwise we end the cycle and display the amount on the display or print.

Unconditional cycle

It is usually used in algorithms when the required number of loop executions is known in advance, and is very often used when working with arrays.

Such an algorithm contains three required elements:

  1. The starting value, which is called the parameter of the cycle, because it is this variable that changes after each execution of the cycle and determines the moment of its completion.
  2. The value at which the loop ends.
  3. Cycle step

what is a cyclic algorithm

At each step, the program checks to see if the starting value exceeds the final value. And if so, the cycle ends. Otherwise, we add the step value to the starting value and the cycle repeats. It should be specially noted that any unconditional cycle can be replaced by a conditional one with a pre- or postcondition.

When compiling cyclic algorithms, two necessary conditions must be adhered to. First: to end the cycle, it is necessary that the contents of the body affect the post- or precondition, otherwise we can end up with an endless cycle. But for some software tasks such cycles are used. As an example of cyclic algorithms that run endlessly, you can cite the Windows operating system, which uses an endless cycle of polling the mouse position to determine user actions. Second: variables passed to the loop must provide at least one of its execution.

Factorial calculation

To consolidate what we read, we give an example of cyclic algorithms for calculating the factorial of an integer. The given example is a cycle with a precondition, but it is possible to implement any kind of cyclic algorithm.

  • Initial data: data - an integer for which the factorial is determined.
  • System variables: parameter of cycle i, taking values ​​from 1 to data in steps of 1.
  • Result: the factorial variable is the factorial of the number data, which is the product of integers from 1 to data.

example of cyclic algorithms
Consider the algorithm in steps:

  1. The algorithm received the number data, for which it is necessary to calculate the factorial.
  2. The factorial variable in which the final result will be stored is set to unity.
  3. We organize a cycle with parameter i and a start value of 1. The final value will be the original number data. As soon as the value of counter i is greater, the cycle ends.
  4. A factorial calculation cycle is performed - the current values ​​of factorial and counter i are multiplied.
  5. We add one to the value of the counter, check the condition of the cycle, and if the result is positive, we complete it.
  6. After the last iteration of the loop, the value of the factorial data! remains in factorial and displayed or printed.

Source: https://habr.com/ru/post/K7475/


All Articles