Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  If
If statement

It branches execution depending on the condition.


Table of contents:


Format

if

elseif

else

Conditional expression

Scope

Statement

endif

Example

Format

The format of the if statement 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 conditional expressions do not meet the condition and there is an else section, the else section is executed.

If 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 format of the if section is:


if ( Conditional-expression )

...


Only one if section is required at the beginning of the if statement.

elseif

A keyword that indicates that the start of an elseif section.

The format of 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 if statement.

else

A keyword that indicates that the start of an else section.

The format of the else section is:


else

...


The else section is optional. Only one can exist in an if statement.

Conditional expression

There are two expressions that can be a conditional expression.


An expression that returns a bool class.

An expression that returns a number: int, long, and real class.


If it returns a bool class, the statement in the section will be executed if true.

If it returns a number, the statement in the section will be executed if it is not zero.

If the conditional expression returns a proxy class, the entity of the proxy is automatically used. The result is the same as calling the Entity getter of the proxy class.


proxy pro = new proxy(true);

if(pro) // pro.Entity is used.

...

endif


Since it is used as a conditional expression, the entity of the proxy must be a bool class or a number.

Scope

There is no scope for the entire if statement. Sections generate separate scopes.

Statement

The statements within a section are optional. You can place statements as many as you want.

endif

It 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.

Next
Previous
Copyright © Rice All rights reserved.