JavaScript loops: for, while, do-while

The history of the formation of the modern syntax of a programming language is akin to cognition of the processes of formation of the Universe. What and how it was at the beginning ... But now everything is simple and affordable.

Javascript loops

The algorithm in the end is always a sequential chain of commands. Parallelism in programming is a combination of somehow combined sequences. A cyclic algorithm has never been more practical than a sequential or parallel chain of commands. Labels, transitions and conditions - everything was enough for any solution. Functional languages ​​deprived these ideas of significance, but the need to repeat sections of code remained.

Browser: DOM, its language + server

In JavaScript, loops remained, although functional ideas took on a special meaning. Something may be left over from Lisp and Prolog, but most likely the area where JavaScript resides has led to what is, but it is doubtful whether this is the last solution.

While javascript

JavaScript runs inside the browser, which receives the page, parses it in the DOM, and runs the first script. All other pages, including those loaded on this page, are the work of the developer, who manipulates the language by which the code on the server can be called and the result obtained using the AJAX mechanism.

The browser executes JavaScript code that can use browser objects, including one that provides information to the server and receives a response, which can be either HTML markup, styles, and the code itself. The answer can be represented by arrays and objects. The meaning of using loops in JavaScript is lost, there are plenty of opportunities to do without them, and risking hanging the browser with an endless sequence of commands is not the best solution.

Loops themselves are present in most JavaScript syntax constructs; a developer can add standard constructions with their own functions.

JavaScript position in code space

The modern programmer does not even think that the cycle he uses (for, while, do while, ...) is ultimately a series of processor cycles (cycles), a simple sequence of binary operations interrupted by counter checks, that is, conditions.

do while

As such, there is no cycle at the level of a machine language: there is a combination of ordinary commands, conditional operations, and transitions. At a higher level, whatever tool is used to develop a browser and JavaScript interpreter, loops will be unambiguous. Moreover, "pieces of code" will be represented by different times and different generations of programmers. A floor above is the JavaScript building. The syntax of which offers modern JavaScript loops.

JS is an excellent language: practical, modern and fully functional. The syntax of this tool includes all the designs that have passed the test of time and become the unshakable foundation of any algorithm. But are cycles really necessary? Progress in programming often asked itself fundamental questions, but only in some cases found a solution.

Objective grounds

A cycle can be only two options: by condition or by counter, but in fact (at the lowest level) any cycle is only by condition. In some languages ​​there is a "for each" cycle. In JavaScript, foreach loops are represented by the prop in object construct, but you can use the array.forEach (...) option.

JavaScript for loops

In any case, there are two options: machine code, which ultimately executes all the algorithms of a programmer, even writing in interpreting languages, has no other options for repeating a chain of commands: he can execute something again until:

  • counter counts;
  • while the condition is satisfied.

JavaScript is a typical interpreter. Its peculiarity: it operates inside the browser, uses its objects and allows you to execute algorithms on the client side, both when loading a page into the browser, and in the process of its operation.

JavaScript foreach loops

A simple loop for everyone

In JavaScript, foreach loops look like applying to a function array:

Example for each

The use of such cycles does not cause difficulties. Formally, there is no such cycle. There is a sequential call to the array elements.

Counter Cycle

For loops look more familiar in JavaScript:

Example for (i ...)

Here the counter is a variable whose value changes according to the formula and the condition is the sign of the end of the cycle. It is not necessary that the formula and condition include a loop variable. But control over the end of the cycle is completely determined by their content.

Conditional loops

The while option with JavaScript suggests depending on when the condition needs to be checked. If the body of the cycle can not be executed even once - this is one thing, if the body must be executed at least once, this is another:

While loops

In the first case, interpreting the while construct, JavaScript first checks the condition, and if it is true, it executes the loop. In the second case, the loop will be executed first. If, as a result of changing the variables specified in the do while construct condition , it takes a false value, the loop will stop executing.

Massive combinations of simple algorithms

The main task (component) of any algorithm is to find, only then make a decision about what to do next. The most primitive search option is accessing a variable, the result is obtained directly. If there are many variables, or it has many values ​​(array), then to select the value, you need to find something that will determine the further behavior of the script.

JavaScript loops examples

Such a simple doctrine made the loop with a counter in JavaScript a kind of panacea for all tasks. Modern computers are fast. There is plenty of time to execute scripts in the browser, there is no hurry. It’s as simple as going through something for something. As a result, for J avaScript for loops have become very popular.

There seems to be nothing wrong with this. But behind this approach, the essence is easily lost, for the sake of which this or that algorithm is written. Data is not pointless. Everything for which any program is written makes sense. Excessively using for loops in J avaScript, a developer may not be able to make out the necessary entity and create an adequate algorithm.

JavaScript counter loop

Functionality, another reflection of reality

Using JavaScript loops, examples of the same type of code can be represented by functions - the algorithm will immediately transform, the main body of the script will decrease in size, everything will become readable and understandable.

This is not a radically new solution, but in essence it does not go beyond other language constructs. In particular, J avaScript loops can be found in the classic split () function:

var cResult = '9.8,7,6,5,4' ;
var aResult = cResult . split ( ',' );

There is no loop here, but how else could this function be performed , if not by searching for the ',' character and using it to separate one number from another.

Abstracting from how this is implemented inside the split () function, you can supplement JavaScript with your own functionality that uses loops, from the point of view of use - more convenient. It is significant that this approach leads to the development of functionality for each task, respectively, but the general will still be with this approach.

Popular while-based function

These functions allt (), padc (), padl () and padr () are something that are not in JavaScript, but sometimes you need to remove spaces from the string or align the length of the string to the left, right, or on both sides. The body of these functions is JavaScript loops. Simple, affordable and never hang an algorithm that uses this.

Popular do-based function

Variants of the functions for converting numbers from hexadecimal to 10th calculus and vice versa, more simply, from one data format to another, are performed here using do while loops. Very compact and efficient language syntax.

javascript loops examples

Correct cycles - reality mapping

JavaScript is not a match for other programming languages ​​and is not distinguished by a variety of versions, and most importantly, it seeks not to change the syntax, but to develop and expand it.

The thinking of a programmer using JS differs from the thinking of a PHP programmer (in particular, other languages ​​in the aggregate, well, except that the "Prolog" and its followers are not included in the general mainstream), when the algorithm is not limited to variables, arrays, assignment operators, cyclic constructions .

If you imagine that there are no cycles, but you need to solve the problem, then the simplest option (blindfold) is to assume that the program processes the data, which is a point or a system of points in the information space. What is a point and what is a system of points is a matter of a specific subject area. For the programmer, this thesis means: there is a simple given and there is a collection of simple data. Naturally, a simple data of one level for the level below will be a system, and for a level above it will be a point.

With this approach, the concern of the point is to manifest your essence through your methods. When a point is in a supersystem, then the function of the system is to manifest its essence as a totality of the entities of its constituent points.

This approach is old as the idea of ​​programming languages, but still has not found its adequate reflection in programming. Many programmers think correctly, but the result of their work leaves much to be desired.

It is sometimes useful to put on a blindfold to see the world!

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


All Articles