Introduction To Ruby
If Statements

Example 1

An if statement executes code if a boolean expression evaluates to true. The following example shows two ways to write this.

# If Statements

x = 10

# In one line
if x==10 then puts "x is 10" end

# Multiple statements executed if condition is true
if x==10 then
  puts "Breaking News"
  puts "x is equal to 10"
end

gets

In the second version, the statements inside the if statement are indented by two spaces. This is the convention for Ruby programming. It makes it clear which statements depend on the if expression and results in code that is easier to read.

Example 2

If the expression is not true, we use an else clause to specify alternate code to execute.

# If ... else

x = 10

if x==10 then
   puts "Breaking News"
   puts "x is equal to 10"
else
   puts "x is not equal to 10"
end

gets

Change the value of the variable after you have tested this script and then run it again to see the else clause execute.

Example 3

We can examine as many conditions as we like within the same if structure using an elsif clause.

# If ... elsif...else

x = -2

if x==10 then
   puts "Breaking News"
   puts "x is equal to 10"
elsif x < 0 then
   puts "x is negative"
else
   puts "x is not equal to 10"
end

gets

If you have experience of programming in other languages, you would be forgiven for thinking that elsif was a spelling mistake either on this site or by the creator of the language. Ruby was based in part on the Perl scripting language, which also spells the keyword that way. Python and Bash scripts use elif. The reason for this is to separate it from else if which is another if inside an else clause - not the same concept but legal in most of these languages. Alternate spellings for elseif exist in lots of C-family languages.

Challenges

  1. Write a program that accepts two numbers as input from the user. Allow the user to specify whether to add or multiply the two numbers.
  2. Adapt your first program to include all four basic arithmetic operations. Use elsif statements to make it happen.
  3. Write a program that allows the user to input 2 numbers, A and B. Output one of the following statements.
    • A is greater than B
    • A is less than B
    • A and B are equal
  4. The following table shows a list of ages and one of the legal rights that a person gets when they reach that age. Use this information to write a program where the user can enter their age and see a list of the legal rights they have.
    • 12: See a 12A-rated film at the cinema without an adult.
    • 14: Have a job which can be described as 'light work'.
    • 15: See a 15-rated film at the cinema without an adult.
    • 16: Enter a pub on your own. You cannot buy alcoholic drinks.
    • 17: Donate blood.
    • 18: Serve on a jury.
    • 21: Adopt a child
  5. Write a program that simulates the rolling of two dice. If the player rolls a double, they get to roll one die as a bonus. The player's final score is the total of all of the dice rolls.
  6. Write a program that simulates the rolling of two dice. If the player rolls a double, they get an extra roll of a single die. If they roll an even number, the number is doubled and added to their previous total. If they roll an odd number, the number is simply added to their previous total.
  7. Write a program that requires the user to enter a number corresponding to a month of the year. If they enter 2, ask them to enter a Year. Check if the year is a leap year or not and report how many days there will be in the month.

For the last two challenges, you will need to write if statements inside if statements. This is called a nested if. Make sure you indent the extra two spaces and your code should be easy to read.