Skip to content
justinmann edited this page Jan 8, 2017 · 10 revisions

The syntax is very C-like, but the () around the condition is not used and the block must be surrounded by {}

if x == 0 {
	"hi"
} else {
	"bye"
}

The if statement can return a value, e.g. to choose between two possible values--hi and bye--write it like this:

result : if x == 0 { "hi" } else { "bye" }

Or if you want to make a more complex decision with an else if:

result : if x == 0 { "hi" } else if x == 1 { "hello" } else { "bye" }

If the else block is not used, then it will use the default value of the return type for the if block

x : 1
result : if x == 0 { true } // result will be false which is the default value for type bool
result : if x == 0 { 1 }    // result will be 0 which is the default value for type int
result : if x == 0 { 1.0 }  // result will be 0.0 which is the default value for type float
result : if x == 0 { "hi" } // result will be "" which is the default value for type string

Clone this wiki locally