Please sign in to comment.
Explain ternary + if statements
- Loading branch information...
Showing with 48 additions and 0 deletions.
|@@ -0,0 +1,29 @@|
|+## `if` statement|
|+The `if` statement is used for when we want to conditionally run code. A common `if` syntax is this:|
|+ if condition then|
|+If `condition` in this example is not `nil` or `false` then the `conditional_code` will be executed. The `then` after the condition can be implicit and doesn't need to be there. Because this `if` is short, it can be all put on to one line:|
|+ conditional_code if condition|
|+You may also use the `else` keyword after the `if` but before the `end` to define code that should be ran if the `if` condition evaluates to `nil` or `false`:|
|+ if condition|
|+A shorter way to write this would be to use a _ternary statement_, which looks like this:|
|+ condition ? conditional_code : alternative_code|
|+In this example the `?` indicates the beginning of the code to run when the `if` evaluates to `true`. After that code, we use `:` to denote what code should be ran if the `condition` evaluates to `false`.|
|+**Please note**: `if` statements that can be condensed to one line will be output as ternary examples, where as multi-lined `if` statements will be output as such with `then` after the condition.|
|@@ -0,0 +1,8 @@|
|+this ? that : them|