Control-flow in computer programming refers to code (statements, methods, etc..) that can be executed upon a client’s (program user’s) request when there are more than one alternative path to a desired action.
A quick example of control-flow in action is if we sold shoes. When the client decides on a style and brand of shoe, there are associated options that are available, options that help the end user tailor an order for him/herself. Attributes such as shoe-size and shoe-color are a necessity to completing an order, control-flow is the technique used to manage these options in our code.
Ruby control-flow consists of the following techniques:
- Conditional execution: Where execution depends on the truth of an expressions
- Looping: Where a single segment of code is executed repeatedly
- Iteration: Where a call to a method is handed a segment of code and can call one or more times during execution
- Exceptions: Where error conditions are handled by special control-flow rules
Today I’ll cover the first two control-flow techniques by sharing how to implement code with the tools associated with it’s respective technique.
Game over if player’s life meter runs to zero, redirect to home page if user password is correct, render up-to-date if its been less than 3 days since latest update of a journal log, etc…
The opportunities to execute specific segments of code if certain conditions are met can be endless. Ruby provides
case statements to give us programmers the proper tools to this end.
if statement looks like this:
Basically an if statement checks to see if the condition is true, and if so, returns the value in the conditional’s body. If the condition returns a false value, then nothing is executed from the body of the
You can also place an entire if statement onto a single line like so:
Both of these lines provide identical results. The flexibility of Ruby’s syntax allows us to substitute semi-colons (mandatory in other languages), for
then and bypass the semi-colon before
Lets say we wanted to have a response for when the condition was true and when it was false.
We could do something like so:
Now we’re getting the hang of things! What happens when we have more than two responses to a condition though? When this is the case, we can use Ruby’s
elsif to make this a reality like so:
Above, we used two
elsif keywords and placed additional program statements to execute should the variable
first_name be true for the additional conditions. One for Harold and one for our President Obama. If none of the strings in the
elsif keywords return
true for its equality to the name stored in the
first_name variable at the top, then we added an exit strategy in the form of a last
else keyword containing execution instructions within its body.
Now this is pretty cool, but in a day-to-day situation, writing
if, elsif, else over and over can become annoying. Wouldn’t it be neat if there was an additional tool for multi-statement conditionals? Well there is, its called a
A case statement starts with an expression, typically either one object or one variable. And walks it through a list of possible matches. Matches are contained in a
when statement. To better illustrate this, lets say we are using a child’s birth year to determine how much their allowance should be.
We can see what this could look like below:
There are more conditional keywords associated with
if that I have not mentioned, and if you’re interested in learning more about them as well, you can have a look here.
Ruby allows us to incorporate conditional logic inside of loops. We, for instance, could loop
while a condition was true, or loop
until a condition became true. We can also break out of loops, terminating the loop at a particular point.
Heres a quick example of looping:
This above example says this. With 1 assigned to the variable
number is less than 11, show us the actual number and increment the variable
We could do the inverse of this same action with the
until keyword like so: