Stenway Developer Network

Conditional Statements

If

If:

if true
  this.someMethod()
end

If / else:

if i == 0
  this.methodA()
else
  this.methodB()
end

Else if

if i == 0
  this.methodA()
else if i == 1
  this.methodB()
else
  this.methodC()
end

Single-line statements can be shortened to:

if true : this.someMethod()
if i == 0 : this.methodA()
else : this.methodB()

Or mixed:

if i == 0 : this.methodA()
else if i == 1 : this.methodB()
else
  this.methodC1()
  this.methodC2()
end

And, Or, Not

if !true : return
if x > 2 && x < 10 : this.doSomething()
if x == 2 || x == 4 : this.doSomething()

Switch Statement

switch i
  case 0
    this.methodA()
  end
  case 1
    this.methodB()
  end
  default
    this.methodC()
  end
end

Or short:

switch i
  case 0 : this.methodA()
  case 1 : this.methodB()
  default : this.methodC()
end