Tracks
/
Ruby
Ruby
/
Syllabus
/
Conditionals
Co

Conditionals in Ruby

22 exercises

About Conditionals

Ruby has what is known as flow control expressions, these are used to control the way the program will run and they take a truthy or falsey value. There are operators that can be used to create truthy or falsey values, these are known as comparison operators.

There are two main control expressions that are used to control which code will run and which will not. Also known as which given branch will run.

Those two are: if and the unless expression.

Comparison operators

Comparison operators are used to compare values and return a true or false value. The following operators require two values to be compared of the same type. If the values are not of the same type then the compiler will throw an error. Here is a list of the operators and an example of when they give a true value:

Method Description Example
< less than 4 < 5
<= less than or equal 4 <= 4
> greater than 3 > 1
>= greater than or equal 2 >= 2

The equal and not equal operators can be used to compare any type of value contrary to the operators already mentioned. The == operator is used to check if two values are equal, and that includes checking the type of the value. The != works the same way but it will return true if the values are not equal and false if they are equal. Here is a list of the equal and not equal operators and an example of when they give a true value:

Method Description Example
== equal 4 == 4
!= not equal 5 != 4

Combined comparison operator

The combined comparison operator (sometimes called spaceship operator) is a special comparison operator. It is special in the sense that it doesn't return a truthy or falsey value but it returns a number. It is written as <=> and it is used to compare 2 values. It will return 1 if the left value is greater than the right value, -1 if the left value is less than the right value, and 0 if the values are equal.

1 <=> 2 # => -1
2 <=> 2 # => 0
3 <=> 2 # => 1

If statement

The if statement is used to check if a given condition is "truthy" or "falsey". If the condition is truthy then the code inside the if statement will run. An if statement ends with the end keyword.

value = 1
if value == 1
  "1 is equal to 1"
end
# => "1 is equal to 1"

if value > 2
  "1 is greater than 2"
end
# => nil

Unless statement

The unless statement works very similarly to the if statement but it will run the code inside the unless statement if the condition is falsey.

value = 1
unless value == 1
  "1 is not equal to 1"
end
# => nil

unless value > 2
  "1 is not greater than 2"
end
# => "1 is not greater than 2"

Else statement

The else statement can be used in conjunction with the if statement. The else statement will be executed if the if branch is not executed.

value = 1
if value == 1
  "1 is equal to 1"
else
  "1 is not equal to 1"
end
# => "1 is equal to 1"

if value > 2
  "1 is greater than 2"
else
  "1 is not greater than 2"
end
# => "1 is not greater than 2"

"Cascading-if" statements

The elsif statement can be used in conjunction with the if statement. The elsif statement will be executed if the if branch is not executed and the condition of the elsif statement is truthy. Elsif statements can be chained together and the first truthy condition will be executed. There can also be an else statement at the end of the if statement which will run if none of the earlier statements have been true.

value = 1
if value == 0
  "1 is equal to 0"
elsif value > 2
  "1 is greater than 2"
else
  "1 is not equal to 0 and 1 is not greater than 2"
end
# => "1 is not equal to 0 and 1 is not greater than 2"

if and unless as suffix

The if and unless statement can also be used as a [suffix][if-as-suffix], this is useful when you want to run a single line of code if a condition is true. It is done by putting the if or unless statement after the code that you want to run.

value = 1
"1 is equal to 1" if value == 1
# => 1 is equal to 1

"1 is not equal to 1" unless value == 1
# => nil
Edit via GitHub The link opens in a new window or tab

Learn Conditionals

Practicing is locked

Unlock 10 more exercises to practice Conditionals