Home About Rice Downloads Documents Others Cooker CookerGX
Documents  >  Tutorial  >  whilestatement
while statement

Purpose of this chapter.

We will learn about the while statement.

Condition loop.

The while statement is a statement to control the loop depending on the condition.

The while statement is the following format.


while ( conditional-expression )

statement...

endwhile


The conditional-expression must be an expression that returns an instance of a bool class. If the conditional expression is evaluated as true, the statements within the while statement will be executed from the beginning. If the condition expression evaluates as false, the execution control will move to the next statement of endwhile.

If the execution control reaches to the continue statement or endwhile, the execution control will move to the conditional-expression. And, repeats a loop from the evaluation of the conditional-expression.

If the execution control reaches to the break statement, the execution control will move to the next statement of endwhile.

The while statement must be end with the endwhile keyword.

while keyword

The 'while' keyword indicates that this statement is a while statement.

Conditional expression.

the conditional-expression must be an expression that returns an instance of a bool class. Also, it must be enclosed by parentheses.

If the conditional-expression evaluates as true, the execution control will move to the next statement of the conditional-expression. And, a loop will be run again.

The end of the while statement.

The end of the while statement is represented by a endwhile keyword.

Scope

The while statement will generate a scope. Range of the scope is from the while keyword to the endwhile keyword.

Variable that has been declared in the while statement can be referred only within the while statement.

Variables that are declared within the scope is cleared every time a new loop begins and will be declared again.

Every time a variable declared, The variable will be assigned the new instance. Also, it does not carry over the previous value to the next loop.

Reserved variable.

If the control reaches to the while keyword, a scope will generate. And, a while statement automatically defines the reserved variable __count.

__count is a variable of int. It is initialized with the number of loops that starts from zero.

__count does not cause the overflow like a normal int. If __count has exceeded the maximum value of the int, it will be automatically back to zero.

Please be careful when using the reserved variable in the conditional expression. It must be especially careful when the loop statement is nested. If the loop statements are nested, reserved variable of internal loop statement hides the reserved variable of external loop statement.

The conditional expression of the while statement is included in the scope that the while statement generates. Therefore, reserved variable in the conditional expression is the reserved variable that is defined by the while statement itself.

Resumption position of the loop.

The conditional expression of the while statement is evaluated at the beginning of the every loop. Therefore, the resumption position of the loop of the while statement is the conditional expression.

Next
Previous
Copyright © Rice All rights reserved.