Difference between revisions of "Ruby Flow Control"

From Techotopia
Jump to: navigation, search
(The Ruby if Statement)
(The Ruby if Statement)
Line 7: Line 7:
 
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:
 
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
+
if ''expression'' then<br>
&nbsp;&nbsp;''ruby code''
+
&nbsp;&nbsp;''ruby code''<br>
 
end
 
end

Revision as of 19:04, 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