June 2015
Intermediate to advanced
374 pages
7h 19m
English
Conditional statements and loops allow developers to branch off from the serial flow of execution of code and also iterate through the code. Ruby provides multiple ways to do this job. Let's look at a few of them.
The
if statement is pretty much a basic branching statement that's provided by many programming languages. It's pretty much how we use the "if" statement in natural language—if it's true, do this; if it's not, do something else.
x=2 if x == 2 puts "True" else puts "False" end
If we need to check for multiple conditions, we get an elsif statement, that we can embed between the if and else statements:
height = 164 if height > 170 puts "Tall" elsif height > 160 puts "Normal" else puts "Dwarf" ...
Read now
Unlock full access