ComputersProgramming

Basic types and example of cyclic algorithms

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

The concept of an algorithm

An algorithm is a sequence of actions for achieving a solution of some computational and other problem in a finite number of steps. Actions (instructions) for the execution of the algorithm can be executed 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 areas of activity, for example, in the management of production and business processes.

Cyclic algorithms

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

Types of cyclic algorithms

Each cyclic algorithm has a cycle condition in its composition, that is, a logical expression whose result determines whether the loop body will be executed again or the loop will be terminated. By the method of processing, all cyclic algorithms are divided into three groups.

Cycle with precondition

In such cyclic algorithms, the continuation condition is checked before processing the loop body, that is, there is a need to repeat the loop processing.

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

Elements of the algorithm:

  1. Set the initial value of the base variable j to -5.
  2. We check the condition of the cycle. The condition is positive, and the loop body is executed for the first time.
  3. Then we add 1 to the variable j, we again check the condition of the cycle.
  4. The loop continues to execute as long as j is less than or equal to zero, otherwise exit the loop on the FALSE branch

Cycle with postcondition

The condition check is performed after the first processing of the loop body and controls the output from it.

Let's analyze the calculation of the sum from 1 to the number n as an example of cyclic algorithms in which the postcondition is used:

  1. We enter a finite number of the calculation of the sum n and set zero initial values of the total sum sum and the counter of the cycle i.
  2. The loop is executed before the first condition check.
  3. We check the condition of the loop, i.e. the value of the counter i is less than or equal to n.
  4. If the result of the condition is positive, we execute the loop again, otherwise we end the loop and print the sum on the display or print.

Unconditional cycle

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

Such an algorithm contains three mandatory elements:

  1. The starting value, which is called the loop parameter, because this variable changes after each execution of the cycle and determines the time of its completion.
  2. The value at which the loop terminates.
  3. Step cycle.

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

When compiling cyclic algorithms, two necessary conditions must be adhered to. First, to end the loop, it is necessary that the contents of the body affect the post or precondition, otherwise we can end up with an infinite loop. But for some software tasks such cycles are applied. As an example of cyclic algorithms that run indefinitely, we can cite the Windows operating system, where an infinite mouse polling cycle is used to determine the user's actions. Secondly, the variables passed to the loop must provide at least one execution.

Calculation of factorial

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

  • Input: data is an integer for which the factorial is defined.
  • System variables: the parameter of the cycle i, which takes values from 1 to data in step 1.
  • Result: variable factorial is the factorial of the number data, which is the product of integers from 1 to data.

Consider the algorithm in steps:

  1. The algorithm received the number data, for which it is necessary to calculate the factorial.
  2. The variable factorial, in which the final result will be stored, is assigned a value of one.
  3. We organize the loop with the parameter i and the start value 1. The final value is the initial number data. Once the value of counter i is greater, the loop terminates.
  4. The cycle of factorial calculation is performed - the current values of factorial and counter i are multiplied.
  5. To the value of the counter add a unit, check the condition of the loop and, if the result is positive, we terminate it.
  6. After the last iteration of the loop, the value of the factorial data! Remains in factorial and is displayed or printed.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.