MC logo

Conditional I

  Ruby Example Code

<<Parallel Asst. if1.rb Conditional II>>
# Pick a random number.
rno = rand(100) + 1
print "Your magic number is ", rno, "\n"

# Perform all sort of totally uselss test on it and report the results.
if rno % 2 == 1 then
    print "Ooooh, that's an odd number.\n"
else
    print "That's an even number.\n"
    if rno > 2 then
        print "It's not prime, BTW.\n"
    end
end

if rno > 50 
    print "That's more than half as big as it could be!\n"
elsif rno == 42
    print "That's the ultimate magic number!!!!\n"
elsif rno < 10
    print "That's pretty small, actually.\n"
else
    print "What a boring number.\n"
end

if rno == 100 then print "Gosh, you've maxxed out!\n" end
See:Ruby Manual

This is the conventional use of the if in perl. Notice that there is no need for curly braces ({ and }). The body of the if ends with the appropriate keyword, end, else or elsif. The then word is generally optional, though you need it if you want to put start the body on the same line as the if, the way the last statement does.

As in most languages (excluding Python), indenting is not required, and is ignored by the interpreter. Of course, it is wise to indent in a way which reflects the structure of the code.
<<Parallel Asst. Conditional II>>