Difference between revisions of "Ruby Flow Control"

From Techotopia
Jump to: navigation, search
(The Ruby if Statement)
(The Ruby if Statement)
Line 10: Line 10:
 
&nbsp;&nbsp;''ruby code''<br>
 
&nbsp;&nbsp;''ruby code''<br>
 
end
 
end
 +
 +
In the above outline, the ''expression'' is a logical expression that will evaluate to either true or false. if the expression is true, then the code represented by ''ruby code'' will execute. Otherwise the code is skipped. ''end'' marks the end of the ''if'' statement.
 +
 +
Let's look at an example:
 +
 +
<pre>
 +
if 10 < 20 then
 +
    print "10 is less than 20"
 +
end
 +
</pre>
 +
 +
When executed, the script will display the string "10 is less than 20", because the 10 < 20 expression evaluated to true.
 +
 +
If this was any language other than Ruby we would now move on to the next section. Except that this is Ruby we are talking about, so we have more flexibility. Firstly, we can drop the ''then'' keyword and get the same result:
 +
 +
<pre>
 +
if 10 < 20
 +
    print "10 is less than 20"
 +
end
 +
</pre>
 +
 +
We can also place the ''if'' after the ''print'' statement, and in doing so, drop the ''end'' statement:
 +
 +
<pre>
 +
print "10 is less than 20" if 10 < 20
 +
</pre>
 +
 +
Similarly, we can place a colon (:) between the ''if'' expression and the statement to be executed:
 +
 +
<pre>
 +
if 10 < 20: print "10 is less than 20" end
 +
</pre>
 +
 +
The expression to be evaluated can also include logical operators. For example:
 +
 +
<pre>
 +
if firstname == "john" && lastname == "smith" then
 +
print "Hello John!"
 +
end
 +
</pre>
 +
 +
== if and elsif ==

Revision as of 19:19, 19 November 2007

One of the most powerful features of Ruby (and every other programming or scripting language for that matter) is the ability to build intelligence and logic into code. This is achieved through the use of control structures which decide what code is executed based on logical expressions.

In this chapter of Ruby Essentials we will look at how such control structures are built.

The Ruby if Statement

The if statement is the most basic of the Ruby control structures. if statements simply specify a section of Ruby script to be executed when a specified criteria is met. The syntax for an if statement is as follows:

if expression then
  ruby code
end

In the above outline, the expression is a logical expression that will evaluate to either true or false. if the expression is true, then the code represented by ruby code will execute. Otherwise the code is skipped. end marks the end of the if statement.

Let's look at an example:

if 10 < 20 then
    print "10 is less than 20"
end

When executed, the script will display the string "10 is less than 20", because the 10 < 20 expression evaluated to true.

If this was any language other than Ruby we would now move on to the next section. Except that this is Ruby we are talking about, so we have more flexibility. Firstly, we can drop the then keyword and get the same result:

if 10 < 20
    print "10 is less than 20"
end

We can also place the if after the print statement, and in doing so, drop the end statement:

print "10 is less than 20" if 10 < 20

Similarly, we can place a colon (:) between the if expression and the statement to be executed:

if 10 < 20: print "10 is less than 20" end

The expression to be evaluated can also include logical operators. For example:

if firstname == "john" && lastname == "smith" then
print "Hello John!"
end

if and elsif