Computer Science‎ > ‎

## If-Else-Elsif Statements

This is what if-else-elsif statements look like in Ruby. Elsif means "Else If"

if [condition_1]
do_something
elsif [condition_2]
do_something_else
else
do some_default_action
end

Example : Given Three positive numbers, a, b, c; are they sides of an isoceles, equilateral or scalene triangle ? Or do they not form a triangle at all ?

`def kindOfTriangle(a,b,c)`
`     if (a == b) && ( b == c ) && ( a == c)`
`    ``    return "equilateral"`
`    elsif ( a + b < c ) || ( a + c < b ) || ( b + c < a )`
`    ``    return "not a triangle"`
`    elsif (a == b ) || ( b == c ) || ( c == a)`
`    ``    return "isoceles"`
`    else`
`    ``    return "scalene"`
`    end`
`end`

`puts kindOfTriangle(5,5,5)`
`puts kindOfTriangle(10,10,7)`
`puts kindOfTriangle(30,10,10)`
`puts kindOfTriangle(3,4,5)`

The Output is :
equilateral
isoceles
not a triangle
scalene

If you want to run it quickly, you could try it out in the Interactive Ruby Shell (irb) :

`irb(main):017:0> def kindOfTriangle(a,b,c)`
`irb(main):018:1> if (a == b) && ( b == c ) && ( a == c)`
`irb(main):019:2> return "equilateral"`
`irb(main):020:2> elsif ( a + b < c ) || ( a + c < b ) || ( b + c < a )`
`irb(main):021:2> return "not a triangle"`
`irb(main):022:2> elsif (a == b ) || ( b == c ) || ( c == a)`
`irb(main):023:2> return "isoceles"`
`irb(main):024:2> else`
`irb(main):025:2* return "scalene"`
`irb(main):026:2> end`
`irb(main):027:1> end`
`=> nil`
`irb(main):028:0> `
`irb(main):029:0* puts kindOfTriangle(5,5,5)`
`equilateral`
`=> nil`
`irb(main):030:0> puts kindOfTriangle(10,10,7)`
`isoceles`
`=> nil`
`irb(main):031:0> puts kindOfTriangle(30,10,10)`
`not a triangle`
`=> nil`
`irb(main):032:0> puts kindOfTriangle(3,4,5)`
`scalene`
`=> nil`
`irb(main):033:0> `

## If Statements

code  if condition

`def demonstrateIf(x)`
`    puts x`
`    puts (x+1) if x == 5`
`    puts (x+2) if x == 6`
`end`

`demonstrateIf(4)`
`demonstrateIf(5)`
`demonstrateIf(6)`

Output:

4
5            /// 5
6            /// 5 + 1
6            /// 6
8            /// 6 + 2

## Ruby Unless Statement

"Unless" runs a code fragment if the conditional is false.

unless [conditional]
[code_fragment]
else
[code_fragment]
end

x = 10
unless x < 10
puts "Unless1"
else unless x < 5
puts "Unless2"
else
puts "Out of all unless statements"
end

Output:
Unless1

`irb(main):001:0> x = 10`
`=> 10`
`irb(main):002:0> unless x < 10`
`irb(main):003:1> puts "Unless1"`
`irb(main):004:1> else unless x < 5`
`irb(main):005:2> puts "Unless2"`
`irb(main):006:2> else puts "Out of all Unless Statements"`
`irb(main):007:2> end`
`irb(main):008:1> end`
`Unless1`
`=> nil`

## Demonstrating the Ruby Case Statement

Using case with numeric data :

`def testCase(x)        # x is a positive integer`
`    case x`
`    when 0..9`
`    ``    puts "Single Digit"`
`    when 10..99`
`    ``    puts "Double Digit"`
`    when 100..999`
`    ``    puts "Triple Digit"`
`    `` else`
`    ``    puts "More than three digits"    # By default, this will be printed`
`     end`
`end`

`testCase(1)`
`testCase(2)`
`testCase(30)`
`testCase(100)`
`testCase(1000)`

Output:
Single Digit
Single Digit
Double Digit
Triple Digit
More than three digits

Using Case with Strings :

`def testCaseWithString(str)`
`    case str`
`    when "line"`
`    ``    puts "Two Points"`
`    ``when`` "triangle"`
`    ``    puts "Three Points"`
`    ``when`` "quadrilateral"`
`    ``    puts "Four Points"`
`    else`
`    ``    puts "More than four points, or a shape which we can't figure out"`
`    end`
`end`

`testCaseWithString("triangle")`
`testCaseWithString("quadrilateral")`
`testCaseWithString("XYZ")`

Output: