# Ruby Programming/Syntax/Control Structures
## Control Structures
### Conditional Branches

Ruby can control the execution of code using Conditional branches. A conditional Branch takes the result of a test expression and executes a block of code depending whether the test expression is true or false. If the test expression evaluates to the constant false or nil, the test is false; otherwise, it is true. Note that the number zero is considered true, whereas many other programming languages consider it false.
In many popular programming languages, conditional branches are statements. They can affect which code is executed, but they do not result in values themselves. In Ruby, however, conditional branches are expressions. They evaluate to values, just like other expressions do. An if expression, for example, not only determines whether a subordinate block of code will execute, but also results in a value itself. If no block is executed, the if expression results in nil. For instance, the following if expression evaluates to 3:
Ruby's conditional branches are explained below.

#### if expression

Examples:
You can also put the test expression and code block on the same line if you use then:
This is equal to:

#### unless expression

The unless-expression is the opposite of the if-expression, the code-block it contains will only be executed if the test expression is false.
Examples:
The unless expression is almost exactly like a negated if expression:
The difference is that the unless does not permit a following elsif. And there is no elsunless.
Like the if-expression you can also write:
The "one-liners" are handy when the code executed in the block is one line only.

#### if-elsif-else expression

The elsif (note that it's elsif and not elseif) and else blocks give you further control of your scripts by providing the option to accommodate additional tests. The elsif and else blocks are considered only if the if test is false. You can have any number of elsif blocks but only one if and one else block.
Syntax:

#### short-if expression (aka ternary operator)

The "short-if" statement provides you with a space-saving way of evaluating an expression and returning a value.
The format is:
It is also known as the ternary operator and it is suggested to only use this syntax for minor tasks, such as string formatting, because of poor code readability that may result.
This is very useful when doing string concatenation among other things.
Example:
Also, this can be written as:
Assignments can be made as:

#### case expression

An alternative to the if-elsif-else expression (above) is the case expression. Case in Ruby supports a number of syntaxes. For example, suppose we want to determine the relationship of a number (given by the variable a) to 5. We could say:
Note that, as with if, the comparison operator is ==. The assignment operator is =. Though Ruby will accept the assignment operator:
this is not what we want! Here, we want the comparison operator.
A more concise syntax for case is to imply the comparison:
Note: because the ranges are explicitly stated, it is a good coding practice to handle unexpected values of a. This concise syntax is perhaps most useful when we know in advance what values to expect. For example:
If entered into IRB this gives:
Other ways to use case and variations on its syntax may be seen at Linuxtopia Ruby Programming

### Loops
#### while

The while statement in Ruby is very similar to if and to other languages' while (syntactically):
The code block will be executed again and again, as long as the expression evaluates to true.
Also, like if and unless, the following is possible:
Note the following strange case works...
So if local variable line has no existence prior to this line, on seeing it for the first time it has the value nil when the loop expression is first evaluated.

#### until

The until statement is similar to the while statement in functionality. Unlike the while statement, the code block for the until loop will execute as long as the expression evaluates to false.

### Keywords
#### return

return value causes the method in which it appears to exit at that point and return the value specified.
Note that if a method has no return statement then the value of the last statement is implicitly returned.