How to set up smartphones and PCs. Informational portal

For each reverse order 1s. Loops using Boolean expression

Description:

Cycle operator For is intended for cyclic repetition of operators located inside the Cycle - EndCycle construction.

Before the loop starts, the value of Expression 1 is assigned to the variable VariableName . The value of VariableName is automatically incremented on each iteration of the loop. The increment value of the counter each time the loop is executed is 1.

The loop is executed as long as the value of the variable VariableName is less than or equal to the value of Expression 2 . The loop execution condition is always checked at the beginning, before the loop is executed.

Syntax:

Parameters:

Variable_name Identifier of the variable (loop counter) whose value is automatically incremented by 1 each time the loop repeats. The so-called cycle counter.Expression 1 Numeric expression that specifies the initial value assigned to the loop counter on the first iteration of the loop.By The syntax link for the Expression 2 parameter.Expression 2 The maximum value of the loop counter. When the variable VariableName becomes greater than Expression 2, the execution of the For loop statement stops.Loop The statements following the Loop keyword are executed as long as the value of the variable VariableName is less than or equal to the value of Expression 2 .

Cycle For Everyone

Description:

The For each loop statement is for looping through collections of values. Each iteration of the loop returns a new element of the collection. The traversal continues until all elements of the collection have been iterated.

Syntax:

Parameters:

Variable_name_1 The variable that is assigned the value of the next element of the collection each time the loop is repeated.From the Syntax link for the variable_name_2 parameter.variable_name_2 The variable or expression that represents the collection. The elements of this collection will be assigned to the variable_name_1 parameter.Loop The statements following the Loop keyword are executed for each element of the collection.// Statements A statement to be executed, or a sequence of such statements.Abort Allows you to abort the loop at any point. After the execution of this statement, control is transferred to the statement following the End of Loop keyword.Continue Immediately transfers control to the beginning of the loop, where the loop execution conditions are evaluated and checked. The statements following it in the body of the loop are not executed at this iteration of the traversal.EndLoop A keyword that ends the structure of a loop statement.

Cycle Bye

Description:

Loop statement while is intended for cyclic repetition of statements that are inside the structure Loop - End Loop. The loop is executed while the logical expression is True. The loop execution condition is always checked first, before the loop is executed.

Syntax:

Parameters:

boolean expression Boolean expression.Loop The statements following the Loop keyword are executed as long as the result of the logical expression is True. // Statements A statement to be executed, or a sequence of such statements.Abort Allows you to abort the loop at any point. After the execution of this statement, control is transferred to the statement following the End of Loop keyword.Continue Immediately transfers control to the beginning of the loop, where the loop execution conditions are evaluated and checked. The statements following it in the body of the loop are not executed at this iteration of the traversal.EndLoop A keyword that ends the structure of a loop statement.
December 12, 2014 at 01:13 pm

Which cycle is faster? Testing 1C

  • High performance ,
  • abnormal programming,
  • Programming

I have been programming 1C for several years, and then the thought came - “Why not take some kind of training course, suddenly there are some gaps in knowledge that I didn’t even suspect before”? No sooner said than done. I sit, listen to the course, reach the cyclic operators and then the second thought (yes, they don’t often appear to me) - "Which cycle is faster?" We should check.
So I found five ways how you can organize the cycle using 1C.

The first type of cycle, let's call it conditionally "ForPo" looks like that:

For n = 0 by Number of Iterations Loop SomeAction(); EndCycle;
Second view "For each":

For Each ItemCollection From Collection Loop SomeAction(); EndCycle;
The third "Bye":

Bye n<>NumberIterations Loop SomeAction(); n = n + 1; EndCycle;
Then I remembered the assembler youth - the cycle "If":

~LoopStart: If n<>NumberIterations Then SomeAction(); n = n + 1; Jump ~StartCycle; EndIf;
And finally "Recursion"

Procedure RecursiveLoop(n, Number ofIterations) SomeAction(); If n<>Number of Iterations Then Recursive Loop(n+1, Number of Iterations); EndIf; EndProcedure
Naturally, it is not entirely correct to attribute recursion to cycles, but nevertheless, with its help, you can achieve similar results. I’ll make a reservation right away that recursion did not participate in further testing. Firstly, all tests were carried out at 1,000,000 iterations, and recursion drops out already at 2,000. Secondly, the speed of recursion is ten times less than the speed of other loops.

Last retreat. One of the conditions was the execution of any actions in the cycle. First, the empty loop is rarely used. Secondly, the “For Each” loop is used for a collection, which means that the rest of the loops must work with the collection so that testing takes place under the same conditions.

Well then, let's go. The loop body was read from a pre-populated array.


or, when using a "ForEach" loop

TestValue Receiver = elem;
Testing was carried out on platform 8.3.5.1231 for three types of interface (Regular application, Managed application and Taxi).
The numbers are the time in milliseconds obtained using the function CurrentUniversalDateInMilliseconds(), which I called before the loop and after it ended. The numbers are fractional because I used the arithmetic average of five measurements. Why didn't I use Benchmarking? I did not have a goal to measure the speed of each line of code, only the speed of cycles with the same result of work.

It would seem that's all, but - to test so test!
Result for platform 8.2.19.106
On average platform 8.2 is 25% faster than 8.3. I did not expect such a difference a little and decided to test it on another machine. I will not give the results, you can generate them yourself using this configuration. I can only say that there 8.2 was 20 percent faster.

Why? I don’t know, disassembling the kernel was not part of my plans, but I still looked into the performance measurement. It turned out that the cyclic operations themselves in 8.3 are somewhat faster than in 8.2. But on the line
TestValue Receiver = TestArray.Get(n);
that is, there is a significant performance hit when reading a collection item into a variable.

Eventually:
Why all this? For myself, I made several conclusions:

1. If it is possible to use a specialized loop - "For Each", then it is better to use it. By the way, by itself it works out longer than other loops, but the speed of access to a collection element is much higher.
2. If you know the number of iterations in advance - use "For". "Bye" will work slower.
3. If you use the "If" loop, other programmers will obviously not understand you.

Attention! At the end of the article is a link to a video tutorial.
If you have not encountered programming before, then the need to use a cycle in a 1C program is probably not entirely clear. In this article, I'll talk about loop statements, as well as their main purpose.

But first, for clarity, a few explanations about the expressions used in the article:
Iteration is a repetition.
Operator brackets are reserved words, which are always written in pairs, an opening statement and a closing statement. For example: Function - EndFunction, If - EndIf, For - EndCycle, etc.
Loop body- the program code located inside the operator brackets of the loop.
Cycle is a construction that repeats the execution of lines located in the loop body, the number of repetitions depends on the result of the conditions at the beginning of the loop.

If this operator did not exist, then the code inside the loop would have to be written as many times as we need iterations. Imagine if there are 100 lines in a document and you need to sort through them and, say, change the value, and if at the same time their number changes periodically, i.e. they are added and removed. It wouldn't be easy.
The loop allows you to simplify the task of executing repeated lines of code.
In the 1C 8.1 program, there are three loop constructions:
"For.. To.. Loop" - repeats the number of loops from the initial value of the counter to the final specified value, adding 1 to the counter variable at each iteration. Used when the number of iterations is known.

Cycle 1C For

For Counter = 1 By 3 Loop // Loop body End Loop;

Cycle 1C Bye

"While... Loop" - is executed while the calculated value is True. It can be used in cases where the variables for calculating the expression are changed in the body of the loop, or if the collection is iterated over and it has a corresponding method that returns a boolean, which is called at the beginning of the loop.

While Selection.Next() Loop //Body of Loop EndCycle;

Cycle 1C For each

"For Each...From...Loop" loops through the collection from the first to the last element. Writing to a variable (in the example: CurrentElement ) the value of the element.

For Each CurrentElement From ArrayElements Loop // Loop Body End Loop;

Reverse cycle 1C

There is also a reverse loop that can be used to traverse collections in reverse order i.e. bottom to top (from the end). This method may be needed if you need to delete elements of the collection

Number of Items = Array of Items. Quantity(); For ReverseIndex = 1 By Number of Items Loop CurrentItem = Array of Items[Number of Items - ReverseIndex]; EndCycle;

Continue and abort operators are covered in the video, link below.

The algorithms of many programs often involve cyclic repetition of certain actions. 1C in this case is no exception. Cycles in 1C allow you to:

  • Iterate over the elements of the directory;
  • Fill in layout areas;
  • Perform certain actions with a selection of documents;
  • And many many others.

Loop types

In 1C, it is customary to distinguish three types of cycles depending on the set of words included in the construction:

  1. For each "Variable" from "Collection of Values";
  2. For "Variable" = "Start. value "According to" Kon. Meaning";
  3. While "Expression".

Let's consider them in more detail.

For each of

This crawler is suitable for collections of values ​​(selection of documents or reference items, stock). Execution will continue until the last element of the collection has been traversed. The line must contain:

  • A variable that specifies the current element of the collection;
  • Defining a collection of values.

The most common error in this case is shown in Fig. 1

Most often, it occurs when a programmer does not fully understand the difference between an object (document, directory) and a collection (selection) of values ​​obtained using the Select() operator.

For by

In this case, the parameters passed to the string are:

  1. The name of the variable is an iterator;
  2. The initial value of the variable;
  3. The final value of the variable.

The repetition of the block of statements included in the loop body will be carried out until the variable equals or exceeds the final value for the first time. In this case, the iterator will increase by 1 each step. The values ​​are compared before the next step is performed.

This construction is very often used when bypassing tabular parts.

When using this crawler, it is important to distinguish between the number of rows in the tabular section and the index of a single row taken. In the first case, the initial value will be equal to 1, the final value can be obtained using the Quantity() operator. Indexes start at 0 and end at Count()-1. Otherwise, you can get an error (Fig.2).

Bye

There is only one parameter here - a logical expression, which is checked for truth before each next step of the loop. As soon as the boolean expression fails, the handler will complete the traversal.

It is very important to understand that in some cases the expression under test may always be True, thus the traversal will be performed an infinite number of times, hanging the system.

In such cases, it is necessary to register one of two options for interrupting execution inside the loop body.

Sometimes a situation may arise when the truth of the checked expression will never come. This can lead to useless searches for errors in the code and loss of time.

Interrupt execution by pressing a key combination

If you write the UserInterrupt Handling() procedure in the loop body, then at any time during its execution, by pressing the Ctrl + PauseBreak key combination, you can stop its work. In this case, a line will be displayed in the message window (Fig. 3).

In order to avoid unpleasant consequences of their activities, it is extremely useful for programmers to accustom themselves to register this procedure in the processing body.

Conditional interrupt

Quite often, the program algorithm involves stopping cyclic processing if a particular condition is met. You can put this procedure in the loop body using the Break statement.

Correctly written into the program code, this operator is highlighted in red.

Jumping some loop operations

Often in a loop it is necessary to check the fulfillment of a condition and, if this condition is not met, skip the main handler. Such structures can be implemented in two ways:

  • In the first method, we put a condition and inside the If Then End If we write the executable code, if the condition is not met, the code will not be executed accordingly. The obvious disadvantage of this approach is that it is quite cumbersome and in the case of a large number of conditions it is easy to make a mistake where one "If" begins and where another ends;
  • It is much more correct to use a construction where, instead of asserting a condition, its negation is taken (instead of equal, it is taken unequally, etc.) and the Continue operator is placed inside the construction;

The "Continue" statement in the body of the code is highlighted in red and moves the execution of the loop to its beginning.

Top Related Articles