Conditional Statements


Here is if statement syntax.

if condition
  something to be done
end

In the above example, condition is a boolean, or something that, when evaluated, will be a boolean, and between the condition and "end", you type what you want to happen. Here is if/else statement syntax.

if condition
  something to be done
else
  something to be done if the condition evualuates to false
end

The else will only be executed if the if statement isn't run, so they will never both be executed. Here is if/elsif/else statement syntax.

if condition
  something to be done
elsif different condition
  something else to be done
else
  another different thing to be done
end

Still only one of the code blocks will be done, because if the if statement is executed, it skips through all the elsif and else statements that go with it. Here is an actual example.

booleanOne = true
randomCode = "Hi!"
if booleanOne
  puts "I will be printed!"
elsif randomCode.length>=1
  puts "Even though the above code is true, I won't be executed because the earlier if statement was true!"
else
  puts "I won't be printed because the if statement was executed!"
end

Exercise

Change the language value so that the elsif statement is the only thing executed.


test