programming.nbk: Home | Index | Next Page: Ruby: Interators | Previous Page: Ruby: Hashes


 Ruby: if-then-else

if condition [then]
    code
[elsif condition [then]
    code]...
[else
    code]
end

The 'then' seems to be optional.

if can also be used as a statement modifier:

code if condition

causes code to be executed only if the condition is true.

There can be additional conditions:

    if count > 10
      puts "Try again"
    elsif tries == 3
      puts "You lose"
    else
      puts "Enter a number"
    end

programming.nbk: Home | Index | Next Page: Ruby: Interators | Previous Page: Ruby: Hashes


Notebook exported on Monday, 7 July 2008, 18:56:06 PM Eastern Daylight Time