You can use an if expression to execute code when a certain condition holds.
If a conditional expression evaluates to true, the if code is carried out. Otherwise, the code is ignored.
a = 42
if a > 7
# outputs “Yes”
The condition a > 7 is evaluated. When it is true, the statements inside the if are executed and the program outputs Yes.
You can have multiple statements inside a single if expression.
if expressions can be nested, one inside the other.
This means that the inner if is the code part of the outer one. This is one way to see whether multiple conditions are satisfied. Keep in mind that once an if condition fails, the entire block is exited.
num = 16
if num > 7
puts “Greater than 7”
if num < 42
puts “Between 7 and 42”
# Greater than 7
# Between 7 and 42
CLICK ON THE BUTTON BELOW TO GO TO THE RUBY MAIN COURSE PAGE.
© License: All Rights Reserved
CONTACT HOLLA TECH – LEARN SUPPORT