if, unless, ternary
Branching basics.
Branching Basics
Programs need to make decisions. In Ruby, the if keyword runs a block of code only when a condition is truthy.
An if expression evaluates a condition and, if it holds, runs the code between if and end.
age = 20
if age >= 18
puts "You are an adult"
endif / else
Add an else branch to handle the case where the condition is false. Exactly one branch runs.
age = 12
if age >= 18
puts "Adult"
else
puts "Minor"
endAll lessons in this course
- if, unless, ternary
- case/when Expressions
- Truthiness in Ruby
- Guard Clauses