Skip to content

Latest commit

 

History

History
137 lines (114 loc) · 1.67 KB

conditional.md

File metadata and controls

137 lines (114 loc) · 1.67 KB

Conditionals

if

if expression 
  code
end

The code between if and end is executed when the expression evaluates to true.

Examples:

# If x is greater than 5, increment x
x = 6 
if x > 5
  x += 1
end
x #=> 7


if 10 > 5
  puts "10 is greater than 5"
end

else

An if statement can include an else clause to specify code to be executed if the condition is not true:

if expression 
  code
else
  code
end

Examples:

a = 10
b = 5

if a > b
  puts "#{a} is greater than #{b}"
else
  puts "#{b} is greater than #{a}"
end

elsif

elsif is a shortened form of "else if"

if expression_1
  code
elsif expression_2
  code
elsif expression_n
  code
else
  code
end

Examples:

if level == :beginer
 puts "Salary up to 1000 USD"
elsif level == :midle
 puts "Salary up to 1500 USD"
elsif level == :senior
 puts "Salary up to 2000 USD"
else
 puts "Salary up to 5000 USD"
end

unless

unless expression 
  code
end

Same with:

if !expression
  code  
end
  • unless is the opposite of if.
  • unless executes code when expression evaluates to false or nil.

Examples:

x = 3
unless x > 5   # That mean when x not greater than 5
  x += 1       # Increment x
end

case statement

case expression is an alternative to the if-elsif-else expression.

Examples:

score = 9

case score
when 1..4
  puts "Failed!"
when 5
  puts "Pass!"
when 6..7
  puts "Not bad!"
when 8..10
  puts "Excellent!"
else
  puts "Invalid score."
end
date = 5

case date
when 2,4,6
  puts "Working in the morning" 
when 3,5,7
  puts "Working in the afternoon"
else
  puts "Off" 
end