34,333
edits
Changes
→The Ruby if Statement
== 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 code to be executed when a specified criteria is met. The syntax for an ''if'' statement is as follows:
if ''expression'' then<br>
end
In the above outline, the ''expression'' is a logical expression that will evaluate to either true or false. if 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>
When executed, the script code 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: