Version 3 (modified by todd.a, 14 years ago) |
---|
If Statement
The if statement is used to conditionally execute code.
Grammar
if-statement = if <expression> <statements> (else if <expression> <statements>)* [else <statements>]
Each expression is evaluated in order until one evaluates as true. Then the statements belonging to that expression are executed. If no expressions evaluate to true and the else portion is present then its statements are executed. Only one set of statements is ever executed and without an else it is possible that no statements are ever executed.
# Example 1 if name print 'Hello, [name].' # Example 2 if name print 'Hello, [name].' else print "I don't know your name." # Example 3 if x < y print 'x is smaller' else if x > y print 'x is larger' else print 'x and y are the same'