For more flexible loop control, use the break
and continue
directives.
Exit: break
Exit the cycle is possible not only when checking the conditions and, in general, at any time. This feature is provided by the break
directive.
For example, the infinite loop in the example will stop execution when i==5
:
11 | alert( 'Последняя i = ' + i ); |
The execution will continue from the line (*)
following the loop.
Next iteration: continue
The continue
directive terminates the current iteration of the loop. For example, the loop below does not display even values:
For even i
continue
triggered, the block is terminated and control is transferred to for
.
As a rule, continue
and use not to process certain values in a loop.
A loop that processes only a fraction of the values might look like this:
01 | for ( var i = 0; i < 10; i++) { |
03 | if ( checkValue(i) ) { |
Everything is good, but we received an additional level of nesting of curly brackets, without which it is possible and necessary to do .
Much better here to use continue
:
01 | for ( var i = 0; i < 10; i++) { |
03 | if ( !checkValue(i) ) continue ; |
We can usually replace if
with a question mark operator '?'
.
That is, write:
.. Similar records:
In both cases, depending on the condition, either a()
or b()
is satisfied.
But the difference is that the question mark operator is '?'
used in the second record returns a value.
Syntax constructs that do not return values cannot be used in the '?'
Operator . These include most of the constructions and, in particular, break/continue
.
Therefore, this code will lead to an error:
(i > 5) ? alert(i) : (i > 5) ? alert(i) : continue ; |
Tags
Sometimes you need to exit multiple levels of a cycle at the same time.
Imagine that you need to enter the values of points. Each point has two coordinates (i, j)
. The cycle for entering values i,j = 0..2
may look like this:
Here break
used to abort the input if the visitor clicked Отмена
. But the usual call break
in line (*)
cannot interrupt two loops at once. How to interrupt the input completely? One of the ways is to put a label .
The label has the form "имя:"
, the name must be unique. It is placed before the loop, like this:
outer: for ( var i = 0; i < 3; i++) { ... } |
You can also make it on a separate line. A call to break outer
interrupts the loop control with this label, like this:
The continue
directive can also be used with a label. The control will jump to the next iteration of the loop with a label.
Labels can be set including on the block, without a loop:
In the example above, break
jump over some_code
, execution will continue immediately after the my
block, from the line (*)
. The ability to put a label on a block is rarely used. Usually tags are put before a cycle.
Some programming languages have a goto
that can transfer control to any part of the program.
break/continue
statements are more limited. They work only inside loops, and the label should not be anywhere, but higher in the nesting level.
There is no goto
in JavaScript.
Importance: 4
1 | Для всех i от 1 до 10 { |
2 | проверить, делится ли число i на какое-либо из чисел до него |
3 | если делится, то это i не подходит, берем следующее |
4 | если не делится, то i - простое число |
Label Solution:
Of course, it can be optimized in terms of performance. For example, check all j
not from 2
to i
, but from 2
to the square root of i
. And for very large numbers, there are more efficient specialized algorithms for checking the simplicity of a number, for example, a quadratic sieve and a sieve of a number field.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone