Website hosting service by Active-Venture.com
 

 Back to Index

Conditionals

if statements allow you to take different actions depending on which conditions are met. For example:

if city == "Toronto"
   drinking_age = 19
else
   drinking_age = 21
end

This says:

  1. if the city is equal to (==) "Toronto", then set drinking_age to 19.
  2. Otherwise (else) set drinking_age to 21.

true and false

Ruby has a notion of true and false. This is best illustrated through some example. Start irb and type the following:

The if statements evaluates whether the expression (e.g. 'city == "Toronto") is true or false and acts accordingly.

Warning: Notice the difference between '=' and '=='.
'=' is an assignment operator.
'==' is a comparison operator.

Most common conditionals

Here is a list of some of the most common conditionals:

== equal
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to

String comparisons

How do these operators behave with strings? Well, == is string equality and > and friends are determined by ASCIIbetical order.

What is ASCIIbetical order? The ASCII character table contains all the characters in the keyboard. It lists them in this order:

...012...789...ABC...XYZ...abc...xyz...

Start irb and type these in:

Note: Notice how irb can help make things clear. You should make a habit of trying things out in irb first. irb is a great tool, so use it!

elsif

elsif allows you to add more than one condition. Take this for example:

if age >= 60
    puts "Senior fare"
elsif age >= 14
    puts "Adult fare"
elsif age > 2
    puts "Child fare"
else
    puts "Free"
end

Let's go through this:

  1. If age is 60 or more, we give a senior fare.
  2. If that's not true, but age is 14 or more, we give the adult fare.
  3. If that's not true, but age is more than 2 we give the child fare.
  4. Otherwise we ride free.

Ruby goes through this sequence of conditions one by one. The first condition to hold gets executed. You can put as many elsif's as you like.

Example - fare_finder.rb

To make things more clear, let's put this in a program. The program asks for your age and gives you the corresponding fare.

Type this in and run it. It should behave like this:

Warning: Be mindful of the order in which you put elsif's. Only the first condition that is true gets executed. This example demonstrates the danger:

age = 21

if age > 5
   puts "Older than 5"
elsif age > 10
   puts "Older than 10"
elsif age > 20
   puts "Older than 20"
elsif age > 30
   puts "Older than 30"
end

This will output:

$ ./example.rb
Older than 5
$

Here age is both greater than 10 and greater than 20. Only the first statement that holds true gets executed.

The correct way to write this would be:

if age > 30
   puts "Older than 30"
elsif age > 20
   puts "Older than 20"
elsif age > 10
   puts "Older than 10"
elsif age > 5
   puts "Older than 5"
end

Exercises

  1. Re-arrange these characters in ASCIIbetical order:

     2, u, A, 4, w, f, R, y
    
  2. The ASCII table contains all the characters in the keyboard. Use irb to find out wheter the characters "?" lies:

    1. Before 0.
    2. After 9 but before A.
    3. After Z but before a.
    4. After z.
  3. Using your experience the previous question, make a program that accepts a character input and tells you if the character lines:

    1. Before 0.
    2. After 9 but before A.
    3. After Z but before a.
    4. After z.

    Then try the program with the following characters:

        $, +, ?, <, -, ~, :, %, ^, {, [
    

    Sample answers:

    • $ lies before 0
    • < lies between 9 and A
    • - lies between Z and a
    • ~ lies after z

 

  

 

 

2002-2004 Active-Venture.com Website Hosting Service

 
Buy domain name at only $5.95/yr with comprehensive domain hosting services. Domain registration and domain search service from $5.95 Cheap domain name registration and domain transfer service
 

Disclaimer: This documentation is provided only for the benefits of our website hosting customers.
For authoritative source of the documentation, please refer to http://www.ruby-doc.org/docs/