If statement
Format
The format is:
if ( Conditional-expression )
...
elseif ( Conditional-expression )
...
else
...
endif
The conditional expressions are evaluated in order from the top, and first section that meets the condition is executed. If all condition do not meet and there is an else section, the else section is executed.
If some section is executed and the execution reaches elseif, else, or endif, execution moves to the next of the endif.
if
A keyword that indicates that it is an if statement and the start of an if section.
The if section is:
if ( Conditional-expression )
...
Only one if section is required at the beginning of the statement.
elseif
A keyword that indicates that the start of an elseif section.
The elseif section is:
elseif ( Conditional-expression )
...
Note that it is "elseif", not "else if".
The elseif section is optional. There can be any number in the statement.
else
A keyword that indicates that the start of an else section.
The else section is:
else
...
The else section is optional. There can be only one in the statement.
Conditional expression
There are two type expressions that can use as a condition.
An expression that returns a bool class.
An expression that returns a number: int, long, and real class.
When it is a bool, the section is executed if it is true.
When it is a number, the section is executed if it is not zero.
Scope
There is no scope for the entire if statement. Sections generate separate scopes.
Statement
Statements within a section are optional. You can place statements as many as you want.
endif
This is a keyword that indicates the end of the if statement.
You can use the abbreviation "ei" instead of "endif". Note that "ei" is also a keyword.
Example
1: | class example |
2: | open method string someMethod(int arg) |
3: | if (arg > 0) // ♦1 |
4: | return "Greater than zero"; |
5: | elseif(arg < 0) // ♦1 |
6: | return "Less than zero"; |
7: | else // arg == 0 |
8: | return "zero"; |
9: | endif |
10: | em |
11: | open method string someMethod2(int arg) |
12: | if (arg) // ♦2: arg != 0 |
13: | return "Not zero"; |
14: | else // arg == 0 |
15: | return "zero"; |
16: | endif |
17: | em |
18: | ec |
♦1: A conditional expression that evaluates the bool value.
♦2: A conditional expression that evaluates a numerical value.