Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Ruby, If...Else, For, While


  • Please log in to reply
No replies to this topic

#1 RhetoricalRuvim


    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 August 2011 - 10:13 PM

What if you got the task to make a program that would say "hello" ten times? You could write 10 puts statements.

But what if you got the task to change those "hello" s to "hi" s? You would change every one of them? Okay, that's not the worst thing ever.

What if you had to write a program that would display 5 * (0 through 9) ? Would you actually write 'puts 5 * 0', 'puts 5 * 1', etc.? And what if the job, then, was to change it to be actually 4 * (0 through 9)?

There is a WAY easier way to do these things, besides writing 100s of lines of code that would be hard to fix and modify.

  • for loops
  • while loops
  • if...else statements
  • Example Program

for loops
for loops can be used in indexed loops with a starting index, start, and an ending index, stop.

There's also the tracking index variable, index.

[COLOR=#5555FF]for[/COLOR] [COLOR=#997744]index[/COLOR] [COLOR=#8888FF]in[/COLOR] [COLOR=#885544]start[/COLOR]..[COLOR=#882255]stop[/COLOR] 
	...the code to run...  

For example, to puts "hello" 10 times, you would:
for i in 0..9 
	puts 'hello' 

And to output the first 15 products of 5 (not including the 15th one):
for i in 0..14 
	puts 5 * i 

while loops
while loops run while the condition, condition, specified is true.

[COLOR=#5555FF]while[/COLOR] [COLOR=#88FF44]condition[/COLOR] [COLOR=#2222AA]do[/COLOR] 
	...the code to run while that condition is true...  

There's also the break keyword, which tells ruby to break from the loop (meaning, stop the loop).

For example:
while password != "the_password" do 
	puts 'Wrong password; try again: ' 
	password= gets.chomp 

The above code should ask the user for a password, and while the password is not equal to "the_pasword" , the loop would continue.

if...else statements
If the condition, condition, specified is true, do whatever's specified. Else, do something else.

[COLOR=#5555FF]if[/COLOR] [COLOR=#89A142]condition[/COLOR] 
	...do something...  
	...do something else...  
end [/COLOR]

Example: [CODE]if name == 'Bob' 
	puts 'Great name!' 
	puts 'Hello ' + name 
end [/CODE]

You can also use the [COLOR=#5555FF]elsif[/COLOR] keyword to tell ruby to use another [COLOR=#5555FF]if[/COLOR] statement if the first one didn't pass. 

Example: [CODE]if age < 0 
	puts 'You can't be younger than 0 years old, you liar!' 
elsif age == 8 
	puts 'Hey, you\'re eight years old.' 
elsif age > 100 
	puts 'Either you are REALLY old, or you\'re deviating from the correct answer.' 
elsif age >= 50 
	puts 'You are old.' 
	puts 'Well, you are still not that old.' 
end [/CODE]

I hope you got the point. 

[B]The Escape (\) Character[/B]
Also, did you notice the "\'" characters? When you're typing in single-quotes and you need an apostrophe, you have to escape that with the backslash (\) character. You also need to escape double-quotes when they're inside double-quoted strings. 

Another time you might have to use the escape character is when you're using backslashes inside strings. 

[SIZE=5][B]Example Program[/B][/SIZE]

[B]The Code[/B]
[CODE]# Think of a random number between 0 and 1. 
number= rand 
# Multiply that by 100, so it's now between 0 and 100. 
number *= 100                               # Same as number= number * 100 
# Convert that to an integer (whole number). 
number= number.to_i 

user_input= nil 
while user_input != number do 
	if user_input != nil 
		if user_input.to_i > number 
			puts 'Too big number; try again.' 
		elsif user_input.to_i < number 
			puts 'Too small number; try again.' 
			puts 'Correct! You got the number right.' 
		puts 'Enter a number between 0 and 100.' 
	user_input= gets 

for i in 0..9 
	puts 4 * i 

# Wait for return key press. 

The Output
Posted Image

(Fullsize Screenshot)

First Tutorial:
Hello World Introduction

Previous Tutorial:

Next Tutorial:
Manual Trigonometric Algorithm
  • 0

Also tagged with one or more of these keywords: ruby

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download