Control Structures: If:else, unless, by case & loops

Control structures aren’t as hard as they look. Their main function is to provide action in Ruby. If you look at someone’s code for a web application a lot of it will be made up of these control structures. They are defining all of the scenarios that the user could take for a certain function. So for example if you were to write an online test that had a math problem then a field for the answer your users will clearly not all get the same thing, unless it’s a really easy problem I suppose. But depending on the answer they give, you could return different messages to them. Your program will run through your conditional structure until it finds a scenario that is returned as true and will execute the corresponding block of code. It is common to have a catch all at the end that handles all other cases as well.

For example there are two common ways to test an expression. IF statements or UNLESS statements. Their functions are just like the english language; if statements will execute the code block if the expression is true and unless statements will execute while the code block is false.

IF example where a = 10 (assignment operator)

if a == 5
a = 9
end

— a would print 10 because the if statement was false. I can also write this on one line:

if a == 5: a=9 end  … it’s saying if a was 5 then set it to 9 .. but it’s not, it’s 10.

An example with unless:

a=10
unless a==5
a = 2
end

— here a would be 2 because we’re saying unless it’s 5 exactly any other number would make it 2. If it is 5 then it returns nil .

Where conditional structures really come in handy is with if-elsif-else expressions. Note how elsif is spelt. This expression can handle any number of situations. Lets say your variable a is set to 5. You have a box where someone is trying to guess the value of the number and you have three different responses; your number is too low, your number is too high and you got it right.

Answer:

a=5
if a == 5
print "Hooray, you got it!"
elsif a < 5
print "Sorry, you're too low"
elsif a > 5
prit "Sorry, you're too high"
else
print "Try again"
end

— In this scenario else really has no function because if you entered some other value other than a number it would return an error and our last elsif handles all other possible number solutions but you can see how the else block would handle edge cases of our expression.

An alternative to writing if-elsif-else statement is case expressions. We can do the same example above with a case expression like so:

 case
 when a < 5 then puts "less than"
 when a > 5 then puts "greater than"
 when a == 5 then puts "equal to"
 end
 >> equal to

to save a little bit of time when you get comfortable with what the syntax means you can replace then with “:”.

Last thing we will cover here is loops and iterators which go hand it hand. Just like they sound loops allow you to go through a code block over and over again. Common syntax for loops in ruby are while or until.

For example a really simple example of a while loop.

$i = 0
$num = 5

while $i < $num  do
   puts("Inside the loop i = #$i" )
   $i +=1
end

which prints:

Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4

You could do the same thing with an until loop by saying until $i > $num do …

An extremely common loop to see is .each do. Here I can specify a loop on a range for example:

(0..5).each do |i|
   puts "Value of local variable is #{i}"
end

Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

One other thing to mention is a few common statements you can run in your loops; break, next, redo & retry. Break will terminate the loop if called inside the block. Next is a good way to skip something inside your loop, it will jump to the next iteration. Redo does exactly what it says, restarts the iteration. Retry also does what it says but will make more sense when we start calling rescue methods on our blocks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s