continue
continue skips the rest of the current iteration of the innermost enclosing loop and starts the next one. If there is no next iteration (a for loop has no more items, or a while condition is now false), the loop ends normally.
See also: continue command help and Control Flow - continue in the Book.
for x in [1 2 3] { print $x; if $x == 2 { continue }; print " after" }
# => 1
# => after
# => 2
# => 3
# => afterLanguage Notes
continuefollows the same placement rules asbreak: it is allowed only infor,whileandloop, and not inside a closure, not even one that is nested in a loop.[1 2 3] | each {|x| if $x == 2 { continue }; $x } # => Error: nu::compile::not_in_a_loop # => # => × 'continue' can only be used inside a loop # => ╭─[repl_entry #1:1:34] # => 1 │ [1 2 3] | each {|x| if $x == 2 { continue }; $x } # => · ────┬─── # => · ╰── can't be used outside of a loop # => ╰────In an
eachclosure,returnwith no value gives the effect ofcontinue: the closure ends early, andeachdropsnullresults (unless you pass--keep-empty). To drop items before processing them, filter withwherefirst.[1 2 3] | each {|x| if $x == 2 { return }; $x * 10 } # => ╭───┬────╮ # => │ 0 │ 10 │ # => │ 1 │ 30 │ # => ╰───┴────╯[1 2 3] | where $it != 2 | each {|x| $x * 10 } # => ╭───┬────╮ # => │ 0 │ 10 │ # => │ 1 │ 30 │ # => ╰───┴────╯In a
whileloop, remember to update the loop variable beforecontinue, or the loop will never end:mut x = 0 while $x < 7 { $x += 1 if $x mod 3 == 0 { continue } print $x } # => 1 # => 2 # => 4 # => 5 # => 7In nested loops,
continueapplies to the innermost loop only:for i in 1..2 { for j in 1..3 { if $j == 2 { continue }; print $"($i),($j)" } } # => 1,1 # => 1,3 # => 2,1 # => 2,3A
finallyblock of a surroundingtryruns before the loop moves on:for i in 1..2 { try { continue } finally { print $"finally ($i)" } } # => finally 1 # => finally 2