1. Computing
Send to a Friend via Email

Control Structures

Control structures alter the execution path (the "flow") the Ruby interpreter takes. They include conditional expressions such as if, if/else and case, and the various loops.

Everything You Wanted to Know About Control Structures
Here is everything you'll ever need to know about control structures (mainly if statements, loops and exceptions) in Ruby.

Boolean Expressions
A "boolean" expression is any expression that is intended to be evaluatated as true or false. Any expression may be used as a boolean expression, but generally only expressions with comparison operators are used as boolean expression.

Boolean Operators
While simple boolean expressions will get you pretty far, you'll soon find yourself wanting to act on more than one condition. For that you'll need boolean operators.

Conditional Statements
How to use conditional statements in Ruby

The Ternary (or "Conditional") Operator
The ternary (or "conditional") operator will evaluate an expression and return one value if it's true, and another value if it's false. It's a kind of short-hand, compact if statement. It has its uses, but it's also a bit controversial.

The Case Equality Operator
The case equality operator is key to understanding how case statements really work, and is overall a useful operator when you want to make "fuzzy comparisons."

The Case Statement
The case statement is a control structure that is usually quite limited in other programming langauges. However, it's quite powerful and flexible in Ruby.

Ruby has a the usual and expect loops, as well as a number of loops specific to Ruby. It even has a way to define you own types of loops.

Not everything goes right all of the time. When something does go wrong in Ruby, an exception will be thrown. It's up to you to catch them, but if you don't your program will exit.

Throw and Catch
Throw and catch are somewhat related to exceptions, but also related to a language construct that's so hated it may be considered a curse word to some: goto.

The Forgotten Keyword: The 'for' Loop
In most imperative languages, one of the primary loops is the 'for' loop. You may have even learned it early on in your Ruby career, but promptly forgotten about it. It just isn't idiomatic Ruby, you'll almost never see it in production code, but it does have some minor uses.

On 'case' and 'class'
While dynamic programming and duck typing often make it a bad idea to do so, it's sometimes useful to do different things based on the type of a given variable.

Conditional Statements
Conditional statements are what makes computer programs tick. Without conditional statements, all computer programs would follow the same path from beginning to end.

©2014 About.com. All rights reserved.