C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
However: If the expression evaluates to false, the inner block of an if-statement is not reached. The statements are not executed.
Ruby program that uses if-statements
# Some integer variables.
a = 1
b = 2
c = 3
# Test for inequality.
if a != b
puts "1 != 2"
end
# Add and then test for equality.
if a + b == c
puts "1 + 2 == 3"
end
Output
1 != 2
1 + 2 == 3
Here: The statement within the else-block is reached. The if and elseif evaluate to false, so "Z" is printed.
Tip: For performance, putting the condition that matches most often first is fastest. Fewer conditions must be evaluated in total.
Ruby program that uses if, elsif and else
# Two integers.
a = 1
b = 2
# Use if, elsif, else on the integers.
if a > b
# Not reached.
print "X"
elsif a == b
# Not reached.
print "Y"
else
# This is printed.
print "Z"
end
Output
Z
Here: The first statement has no effect because the variable is not greater than 5. But the second statement takes effect.
Ruby program that uses one-line if-statements
# Variable equals 5.
i = 5
# Use one-line if-statements.
puts "Greater than 5" if i > 5
puts "Not greater than 5" if i <= 5
Output
Not greater than 5
Also: There is an else-statement that can follow the unless statement. The syntax is just like an if-block.
Ruby program that uses unless
i = 4
# Use an unless-else construct.
unless i == 3
puts "UNLESS"
else
puts "ELSE"
end
Output
UNLESS
Here: We display the value of the string with puts, unless the value equals Cat. So the value Dog is displayed.
Ruby program that uses unless, one-line
animal = "Dog"
# Display the animal string unless it equals Cat.
puts animal unless animal == "Cat"
Output
Dog
Short-circuit: These operators short-circuit. And will stop checking after its first false result, "or" after its first true.
Note: The English operators (and, or) are easier to read. But the symbolic ones are more familiar to developers of C-like languages.
Note 2: The English operators have a lower precedence. For consistency the C-like operators may be a better choice.
Ruby program that uses and, or
animal1 = "cat"
animal2 = "dog"
# And operators.
if animal1 == "cat" and animal2 == "dog"
puts 1
end
if animal1 == "cat" && animal2 == "dog"
puts 2
end
# Or operators.
if animal1 == "whale" or animal2 == "dog"
puts 3
end
if animal1 == "whale" || animal2 == "dog"
puts 4
end
Output
1
2
3
4
Note: In my testing, this does not affect many simple expressions. But the change in precedence could affect more complex things.
Program: Consider the 2 if-expressions. The first has "and" which means the part after the "||" is evaluated together.
And: The second if-statement has "&&" which means "top" is evaluated all by itself.
Ruby program that shows precedence change
left = 0
top = 2
# Not true because "and" has lower precedence.
if top == 2 || left == 0 and top == 3
puts "1"
end
# True because && has higher precedence.
if top == 2 || left == 0 && top == 3
puts "2"
end
Output
2
Evaluation order:
If 1: (top == 2)
(left == 0, top == 3)
If 2: (top == 2, left == 0)
(top == 3)
Otherwise: The second value is used as the result. Here, the value equals 10, so the result is 20. The 0 is returned in all other cases.
Ruby program that uses ternary
value = 10
# Ternary statement.
result = value == 10 ? 20 : 0
puts result
Output
20
But: The program still compiles. In some languages, this syntax results in a fatal, compile-time error.
Ruby program that causes equals sign warning
value = 10
# We should use two = in a conditional.
if value = 20
puts true
end
Warning
C:/programs/file.rb:6: warning: found = in conditional, should be ==
Output
true
Result: In this program, the variable cat_sound is assigned to "meow" because the size variable is equal to 0.
Ruby program that assigns to if
# Set size to zero.
size = 0
# Assign cat_sound to result of if-statement.
cat_sound =
if size == 0
"meow"
else
"roar"
end
puts(cat_sound)
Output
meow
Note: In programming, clarity is key. But standard forms—using Ruby that other developers also use—is also important.
Ruby program that uses then keyword
value = 10
size = 10
# The "then" keyword is optional.
if value == size then
puts true
end
# Omit the then.
if value == 10
puts 10
end
Output
true
10
Numbers: All numbers (including 0) evaluate to true. Other programming languages sometimes treat 0 as false, but not Ruby.
Empty array: An empty Array evaluates also to true. Empty collections are treated the same as ones with elements.
Nil: False and nil evaluate to false—these were the only false values found in this test.
Ruby program that tests for true, false
values = [0, 1, -1, true, false, nil, Array.new()]
# Test all the values for truth.
values.each do |v|
if v
puts String(v) + " = true"
else
puts String(v) + " = false"
end
end
Output
0 = true
1 = true
-1 = true
true = true
false = false
= false
[] = true
Tip: Thanks to Jason Newell for writing in with an important point about operator precedence.