Home Explore Blog CI



nushell

lang-guide/chapters/flow_control/if-else.md
212f1e7eaf6264e7a9641c7e9d51cf937eb0ce3f9dd1e5c100000003000002b2
# if/else

The `if` expression evaluates a condition and then chooses to run a block based on the condition.

For example, you can print "yes", based on a true condition:

```nu
if true {
  print yes
} else {
  print no
}
```

Alternately, you can print "no", based on a false condition:

```nu
if false {
  print yes
} else {
  print no
}
```

The `else` part of `if` is optional. If not provided, if a condition is false, the `if` expression returns `null`.

The code that follows the `else` is an expression rather than a block, allowing any number of follow-on `if` expressions as well as other types of expressions. For example, this expression returns 100: `if false { 1 } else 100`.

Chunks
7705116b (1st chunk of `lang-guide/chapters/flow_control/if-else.md`)
Title: Conditional Logic with 'if/else' in Nushell
Summary
The 'if' expression in Nushell allows conditional execution of code blocks. It evaluates a condition and executes a corresponding block if the condition is true. An optional 'else' block can be provided to execute code when the condition is false. If no 'else' block is present and the condition is false, the 'if' expression returns null. The 'else' part allows any number of follow-on `if` expressions.