Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Language Reference Guide
    • Readme
    • Types in the Nu Language
      • Basic Types
        • Any
        • Boolean
        • Integer
        • Float
        • Filesize
        • Duration
        • Datetime
        • Range
        • String
        • Record
        • List
        • Table
        • Closure
        • Nothing
        • Binary
        • Glob
        • Cell-Path
      • Other Data Types

        • Types used only in command signatures
          • Path
        • Types which are not declarable
          • Error
          • CustomValue
          • Block
      • Type signatures
      • Commands that interact with types
    • Operators
    • Flow control
      • if/else
      • loop
      • while
      • match
      • try/catch
      • break
      • return
      • continue
    • Filters
      • each and par-each
      • Filters to select subsets of data
      • where and filter
      • Understanding the difference between get and select
    • Custom Commands
    • Declarations
    • Variable Scope
    • Strings and Text Formatting
    • Helpers and debugging commands
    • Pipelines
    • MIME Types for Nushell

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
# =>   after

Language Notes

  1. continue follows the same placement rules as break: it is allowed only in for, while and loop, 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
    # =>    ╰────
  2. In an each closure, return with no value gives the effect of continue: the closure ends early, and each drops null results (unless you pass --keep-empty). To drop items before processing them, filter with where first.

    [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 │
    # => ╰───┴────╯
  3. In a while loop, remember to update the loop variable before continue, 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
    # => 7
  4. In nested loops, continue applies 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,3
  5. A finally block of a surrounding try runs before the loop moves on:

    for i in 1..2 { try { continue } finally { print $"finally ($i)" } }
    # => finally 1
    # => finally 2
Edit this page on GitHub
Contributors: NotTheDr01ds, Kieron Wilkinson, fdncred
Prev
return