Kontynuuj — informacjeAbout Continue
Krótki opisShort description
Opisuje, w jaki sposób continue
Instrukcja natychmiast zwraca przepływ programu do góry pętli programu, switch
instrukcji lub trap
instrukcji.Describes how the continue
statement immediately returns the program flow to the top of a program loop, a switch
statement, or a trap
statement.
Długi opisLong description
continue
Instrukcja umożliwia wyjście z bieżącego bloku sterowania, ale kontynuuje wykonywanie, zamiast całkowicie zakończyć pracę.The continue
statement provides a way to exit the current control block but continue execution, rather than exit completely. Instrukcja obsługuje etykiety.The statement supports labels.
Etykieta to nazwa przypisywana do instrukcji w skrypcie.A label is a name you assign to a statement in a script.
Używanie pętli Continue inUsing continue in loops
Instrukcja bez etykiet continue
natychmiast zwraca przepływ programu do góry wewnętrznej pętli, która jest kontrolowana przez for
, foreach
, do
, lub while
instrukcji.An unlabeled continue
statement immediately returns the program flow to the top of the innermost loop that is controlled by a for
, foreach
, do
, or while
statement. Bieżąca iteracja pętli została zakończona, a pętla kontynuuje z następną iteracją.The current iteration of the loop is terminated and the loop continues with the next iteration.
W poniższym przykładzie przepływ programu powraca do góry while
pętli, jeśli $ctr
zmienna jest równa 5.In the following example, program flow returns to the top of the while
loop if the $ctr
variable is equal to 5. W związku z tym wszystkie liczby z zakresu od 1 do 10 są wyświetlane z wyjątkiem 5:As a result, all the numbers between 1 and 10 are displayed except for 5:
while ($ctr -lt 10)
{
$ctr += 1
if ($ctr -eq 5)
{
continue
}
Write-Host -Object $ctr
}
W przypadku korzystania z for
pętli wykonywanie kontynuuje się w <Repeat>
instrukcji, a po niej <Condition>
test.When using a for
loop, execution continues at the <Repeat>
statement, followed by the <Condition>
test. W poniższym przykładzie pętla nieskończona nie zostanie wystąpiła, ponieważ zmniejsza się $i
po continue
słowie kluczowym.In the example below, an infinite loop will not occur because the decrement of $i
occurs after the continue
keyword.
# <Init> <Condition> <Repeat>
for ($i = 0; $i -lt 10; $i++)
{
Write-Host -Object $i
if ($i -eq 5)
{
continue
# Will not result in an infinite loop.
$i--
}
}
Używanie z etykietą Kontynuuj w pętliUsing a labeled continue in a loop
Instrukcja oznaczona etykietą continue
kończy wykonywanie iteracji i przenosi kontrolkę do dostosowanej iteracji lub switch
etykiety instrukcji.A labeled continue
statement terminates execution of the iteration and transfers control to the targeted enclosing iteration or switch
statement label.
W poniższym przykładzie wewnętrzna for
jest zakończona, gdy $condition
ma wartość true , a iteracja kontynuuje działanie z drugą for
pętlą w labelB
.In the following example, the innermost for
is terminated when $condition
is True and iteration continues with the second for
loop at labelB
.
:labelA for ($i = 1; $i -le 10; $i++) {
:labelB for ($j = 1; $j -le 10; $j++) {
:labelC for ($k = 1; $k -le 10; $k++) {
if ($condition) {
continue labelB
} else {
$condition = Update-Condition
}
}
}
}
Używanie Kontynuuj w instrukcji switchUsing continue in a switch statement
Nieoznaczona continue
instrukcja w ramach switch
zakończenia wykonywania bieżącej switch
iteracji i przenosi kontrolkę na początek, switch
Aby uzyskać następny element wejściowy.An unlabeled continue
statement within a switch
terminates execution of the current switch
iteration and transfers control to the top of the switch
to get the next input item.
Gdy istnieje pojedynczy element wejściowy continue
, kończy całą switch
instrukcję.When there is a single input item continue
exits the entire switch
statement.
Gdy switch
dane wejściowe są kolekcjami, switch
testuje każdy element kolekcji.When the switch
input is a collection, the switch
tests each element of the collection. continue
Kończy bieżącą iterację i switch
kontynuuje przy użyciu następnego elementu.The continue
exits the current iteration and the switch
continues with the next element.
switch (1,2,3) {
2 { continue } # moves on to the next element, 3
default { $_ }
}
1
3
Używanie Kontynuuj w instrukcji pułapkiUsing continue in a trap statement
Jeśli końcowa instrukcja wykonywana w treści trap
instrukcji to continue
, błąd pułapki jest ignorowany w trybie dyskretnym, a wykonywanie jest kontynuowane za pomocą instrukcji bezpośrednio po tej, która spowodowała trap
wystąpienie.If the final statement executed in the body a trap
statement is continue
, the trapped error is silently ignored and execution continues with the statement immediately following the one that caused trap
to occur.
Nie używaj Kontynuuj poza pętlą, przełącznikiem lub pułapkąDo not use continue outside of a loop, switch, or trap
Gdy continue
jest używany poza konstrukcją, która bezpośrednio ją obsługuje (pętle, switch
, trap
), program PowerShell wyszukuje stos wywołań dla otaczającej konstrukcji.When continue
is used outside of a construct that directly supports it (loops, switch
, trap
), PowerShell looks up the call stack for an enclosing construct. Jeśli nie można znaleźć otaczającej konstrukcji, bieżący obszar działania jest cicho zakończony.If it can't find an enclosing construct, the current runspace is quietly terminated.
Oznacza to, że funkcje i skrypty, które w sposób nieumyślnie używają continue
poza otaczającą konstrukcję, która go obsługuje, mogą przypadkowo kończyć swoje obiekty wywołujące.This means that functions and scripts that inadvertently use a continue
outside of an enclosing construct that supports it, can inadvertently terminate their callers.
Użycie continue
wewnątrz potoku, takiego jak ForEach-Object
blok skryptu, nie tylko opuszcza potok, może spowodować przerwanie całego obszaru działania.Using continue
inside a pipeline, such as a ForEach-Object
script block, not only exits the pipeline, it potentially terminates the entire runspace.