Control Flow in Ruby
if
Statements:
if
Statements:An if
statement takes an expression, and runs it to see if it is true or false. If the expression is true
, then it runs the block of code. If the expression is false
, then it moves onto the next piece of instruction.
An example:
If an if
statement is short, and only requires one line, then you can use a simpler syntax. The statement has a syntax like this: expression if boolean
. It would be said like so: Do this expression if the boolean is true or false (which ever one you put). The order they go in is extremely important! If they’re not in that order, it won’t work.
Using the example above, it would look like this:
if/else
Statements:
if/else
Statements:The partner to the if
statement, is the else
statement. An if/else
statement says to ruby: If this expression is true, run this code block; otherwise, run the code block after the else
.
For example:
The more concise version of the if/else
statement is called the ternary conditional expression. It is called ternary because it takes three arguments: a boolean, an expression to evaluate if the statement is true, and an expression for if the statement is false. It has syntax which looks like this: boolean ? Do this if true : Do this if false
. It would be said like so: Evaluate this boolean, if it’s true do the first expression, if it’s false do the second expression. The order they go in is extremely important! If they’re not in that order, it won’t work.
For example:
elsif
Statements
elsif
StatementsIf you need more than just two options, then you can implement an elsif statement.
For example:
unless
Statements
unless
StatementsSometimes you want to use control flow to see if a statement is false, rather than if it’s true. This is where you use the unless statement. Lets say that you don’t want to eat unless you’re hungry, and when you’re not hungry then you want to draw pictures.
You would write something like this:
If an unless
statement is short, and only requires one line, then you can use a simpler syntax. The statement has a syntax like this: expression unless boolean
. It would be said like so: Do this expression unless the boolean is true or false (which ever one you put). The order they go in is extremely important! If they’re not in that order, it won’t work.
Using the example above, it would look like this:
switch
Statements
switch
StatementsWhen you have multiple options, you can use the switch
statement. It allows you to put multiple when
statements, or conditions, and then which ever one is true, it will run that condition. It is exactly like an elsif
statement, but better for situations where there are multiple choices, because we don’t want to use too many elsif
statements.
The syntax looks like this:
However, there is a simpler way to do it, if the conditions don’t have a big block of code. Meaning, if they only have one line of instructions, then you can fold it up into a simpler syntax like so:
Last updated