ComputersProgramming

JavaScript loops: for, while, do-while

The history of the formation of the modern syntax of the programming language is akin to the knowledge of the processes of the formation of the universe. What and how it was in the beginning ... But now everything is simple and accessible.

Algorithm in the end - always a consistent chain of commands. Parallelism in programming is a set of somehow combined sequences. The cyclic algorithm has never been more practical than a sequential or parallel chain of commands. Tags, transitions and conditions - all was enough for any decision. Functional languages deprived these ideas of significance, but the need for repetition of code sections remained.

Browser: DOM, its language + server

JavaScript cycles have remained, although functional ideas have acquired special meaning. Perhaps there is something left of Lisp and Prolog, but most likely the area where JavaScript resides has led to what exists, but it is doubtful that this is the last decision.

JavaScript works inside the browser, which gets the page, parses it into the DOM, and runs the first script. All other pages, including those loaded on this one, are the handiwork of the developer who manipulates the language, through 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 include HTML markup, styles and code itself. The answer can be represented by arrays and objects. The sense of using JavaScript is lost in the JavaScript, there are a lot of ways to do without them, and risking to hang the browser with an infinite sequence of commands is not the best solution.

The actual cycles are present in most JavaScript syntax constructs, the developer can supplement the standard constructs with their own functions.

The position of JavaScript in the code space

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

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

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

Objective grounds

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

In any case, there are two options: machine code, which eventually executes all the algorithms of the programmer, even writing in interpretive languages, has no other options for repeating the chain of commands: it can do something again until:

  • The counter counts;
  • Until the condition is satisfied.

JavaScript is a typical interpreter. Its feature: it functions inside the browser, uses its objects and allows executing algorithms on the client side, both when the page is loaded into the browser, and in the process of its operation.

A simple cycle for everyone

In JavaScript, foreach loops look like an application to an array of functions:

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

Counter cycle

More familiar look at JavaScript for:

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

Conditional loops

The option with while JavaScript offers, depending on when you need to check the condition. If the body of the cycle can not be executed once - this is one if the body must be executed at least once, this is another:

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

Massive combinations of simple algorithms

The main task (the component part) of any algorithm is to find, only then decide on what to do next. The most primitive version of the search is to access the variable, the result is obtained directly. If there are many variables, or it has many values (an array), then to find the value, you need to find something that will determine the further behavior of the script.

Such a simple doctrine made the cycle with the counter in the JavaScript language a kind of panacea for all tasks. Modern computers are fast. There is plenty of time to execute scripts in the browser, there is nowhere to hurry. It's simpler to go over something for something. As a result, for J avaScript, for loops have become very popular.

There is nothing bad in this. But behind such an approach, the essence for which one or another algorithm is written is easily lost. Data is not pointless. Everything for which any program is written, makes sense. Excessively using for J avaScript for loops, the developer can not see the necessary entity and create an adequate algorithm.

Functionality, another representation of reality

однотипного кода можно представить функциями - алгоритм сразу преобразится, основное тело скрипта уменьшится в размере, все станет читаемым и понятным. Using JavaScript-cycles, examples of the same code can be represented by functions - the algorithm will immediately change, the main body of the script will decrease in size, everything will become readable and understandable.

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

cResult = '9,8,7,6,5,4' ; Var cResult = '9,8,7,6,5,4' ;
aResult = cResult . Var aResult = cResult . ( ',' ); Split ( ',' );

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

By abstracting 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 using it - more convenient. It is essential that such an approach leads to the development of a functional for each problem, respectively, but the general will still be with this approach.

These functions allt (), padc (), padl (), and padr () are things 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 both sides. In the body of these functions are JavaScript-cycles. Simple, accessible and never hangs an algorithm using this.

Variants of the functions of converting numbers from hexadecimal to the 10th system of calculus and vice versa, it is easier to say, from one data format to another, are performed here through do-while cycles. Very compact and effective language syntax.

The correct cycles are a representation of reality

JavaScript is not a pair of other programming languages and does not differ in the variety of versions, and most importantly, it seeks not to change the syntax, but to develop and expand it.

The thinking of the programmer using JS differs from the thinking of the PHP programmer (in particular, other languages in the aggregate, except that "Prolog" and its followers do not enter into the common channel), when the algorithm is not limited to variables, arrays, assignment operators, cyclic constructs .

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

With this approach, point care is to manifest one's essence through one's own methods. When a point in the supersystem, the function of the system is to manifest its essence as a collection of entities of the points entering into it.

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

It is useful sometimes to put on a bandage over the eyes to see the world!

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

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