Wednesday, June 28, 2006

Simple Constructs in Ruby

Today, we shall explore some very simple constructs available in Ruby. The example below (Constructs.rb) illustrates the if else end construct.
# if else end
var = 5
if var > 4
puts "Variable is greater than 4"
puts "I can have multiple statements here"
if var == 5
puts "Nested if else possible"
else
puts "Too cool"
end
else
puts "Variable is not greater than 5"
puts "I can have multiple statements here"
end
An example of using elsif is there in the program ElsIfEx.rb

Some common conditional operators are: ==, != >=, <=, >, <

Loops like the while are available. Again, the example below illustrates the same.
# Loops
var = 0
while var < 10
puts var.to_s
var += 1
end

I shall talk about another construct the do end when we discuss arrays.

First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

4 Comments:

Blogger Unknown said...

Robert, you can post your program as a comment to my post

7:20 PM  
Blogger Aspirations said...

Hi Sir,

I am confused why,

puts 'I can have multiple statements here'
and
puts 'I can not have multiple statements here'

Does the comments imply that I really can not have multiple statements at the second place? I tried and it worked or am I missing something?

7:23 PM  
Blogger Unknown said...

Anish, I am sorry the text in the puts statement was confusing. I have changed it. We can have multiple statements both places.

5:13 AM  
Blogger Unknown said...

Sandip, send me your code.

12:58 PM  

Post a Comment

<< Home