ComputersProgramming

JavaScript: switch case - select options

The reason is always one, the values are probably a lot. Or maybe the other way around: for one value there will be several reasons for its appearance. In any case, it is necessary to take only one decision depending on some condition, or vice versa.

Even if the solution can have several options, only one path will lead to any goal, but there are usually several ways, and one must make a choice.

The classical conditional statement if (condition) {algorithm 1 if the condition is true} else {algorithm 2 if the condition is false} gives only two choices. But even a simple real task always gives many options.

A simple "not one", but one condition

JavaScript construction, switch case = multiple condition values. A similar one can be obtained by using combinations of the If statement. However, undoubtedly, the first variant is syntactically and semantically more correct. In addition, formally the decision on the second option will require the developer of greater skill:

Here the combination if (...) {...} else {...} and try {...} cath (...) {...} provides the beginning of the AJAX exchange procedure in the algorithm. The response is processed in a similar set of conditional statements.

JavaScript switch case: example

The main external difference between the choice of the option and one condition: you can write not two but several executable command blocks. Only one or only a few will be performed.

The J avaScript switch case allows you to provide several algorithms, depending on the value of the variable or the value of the expression. It is important to consider that the choice of the desired algorithm will be made by the exact match of the value of the condition in the switch () and the value specified in the case. The symbol "1" and the digit 1 are not the same, type conversion is not performed here.

Executing Multiple Sections

A characteristic feature of the choices in the J avaScript switch case is the absence of block brackets - {...}, which are assigned to determine only the beginning and end of the body of this operator. Each case section is terminated by a break statement, which may not exist.

In these examples, a break was skipped in the first case section. The variant on the left will give the result of Variant2, because exactly how it will start execution, and in the variant on the right the Variant1Variant2 will be received, since the first one will be executed, but there will be no effect from the second condition, and besides, the operators of the first section will not interfere, then The second: between them there is no break statement, and twice the JavaScript switch case does not. It should be noted that changing the variable iCond in the body structure will not have any effect.

The reality of the code and the reality of the problem

JavaScript is a great language, fast, concise, with a well-rounded and logical syntax. The JavaScript switch case works fine, it allows you to clearly and accurately describe the desired variety of options from a particular condition.

Selection operators can be inserted into each other. In their sections, the case can be conditional statements, loops, any other constructs. Do not neglect the scope of variables. Used everywhere should be described globally.

However, you should not abuse JavaScript. Switch case is an excellent branching tool, but not mobile on large amounts of code. Than the more thoughtful and functional the choice of variants is executed, the more difficult it will be to change later.

The real task never stands still, and if we do not take into account the changes in the process of solving it, then from the moment of completion of the work, in the first days of operation of the created resource, inaccuracies and shortcomings will be evident. The task will undoubtedly go ahead, and the site will have to be finalized.

From this perspective, the main drawback of the JavaScript switch case derives from its size and complexity. If you do not abuse this, you can achieve the achievement done by compact and small choices.

Condition + solution = a new turn of movement

A correctly formulated problem approximates the solution. Although the task, by definition, itself can not decide anything and bring it closer.

The condition is used everywhere (private small task). Execution of the condition = execution of some code. Using JavaScript in a switch case is both a tribute to fashion and an obvious necessity. This is done in all languages, because it's simple and convenient.

Once born "Lisp" and "Prologue", similar programming languages to them. There have been battles over transition operators and labels. Tried to get a decent rating of the functional style of writing programs. Today, a mass developer enthusiastically promotes object-oriented ideas.

However, till now in the modern works conditional operators find their essential value. But the essence of modern ideas is precisely that the objects themselves put conditions and find solutions. After all, an object is a collection of syntax and semantics.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

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