Pardon the mess, Play My Code is in beta!

READY TO PLAY?
CLICK TO LOG IN!

sign up - lost password

If Statements

These are used for conditions, to allow different parts of your code to run

The syntax is 'if' and then a condition, and then a series of statements (which is optional). 'end' is used for the end of an if statement.

if player.isDead()
    runGameOver()
end

More branches can then be added using 'else if', or 'else' for a default branch when others fail:

if a > b
    // run code
else if b < a
    // run different code
else
    // default statement
end

To help with compatibility, Quby also supports some slight alternatives. You can use 'elseif' instead of 'else if', and you can optionally use 'then' after the condition.

if a > b then
    // run code
elseif b < a then
    // run different code
else
    // default statement
end

Conditions

The condition for an if statement is considered to be 'true' if the value it evaluated to is not false, and not null. Anything else, such as numbers (including 0), and any strings (including empty strings), is considered true.

/* 
 * A number of if condition tests.
 */

// true : )
testCondition( 0, 'zero' )
testCondition( '', 'an empty string')
testCondition( :foo, 'a symbol')
testCondition( [], 'an empty array')

// false : (
testCondition( null, 'null')
testCondition( 5 == 6, '(5 == 6)')

def testCondition( cond, name )
    if cond
        console( name + ' is true!' )
    else
        console( name + ' is false!' )
    end
end

See Also