Editing While
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
{{Function | {{Function | ||
|origin = NVSE | |origin = NVSE | ||
|summary = Added by NVSE V4. Whilst a specified condition is met, the code section below, until the | |summary = Added by NVSE V4. Whilst a specified condition is met, the code section below, until the 'loop' command, will repeat. Within while loops, the command 'continue' may be used to skip remaining loop code and return to the while line. In the same context, the command 'break' may be used to end the loop immediately, regardless of the while condition. | ||
The same effect could be achieved using | The same effect could be achieved using label, goto, if and endif statements, but this method allows for fewer lines and improved readability. Care must be taken to avoid the possibility of infinite loops- a while condition that never becomes false will immediately crash the game. | ||
|name = | |name = While | ||
|returnType = void | |returnType = void | ||
|arguments = | |arguments = | ||
{{FunctionArgument | {{FunctionArgument | ||
|Name = Condition | |Name = Condition | ||
|Type = | |Type = boolean | ||
}} | }} | ||
}} | }} | ||
<pre> | <pre> | ||
while SomeCondition | while ( SomeCondition ) ; this is similar to an 'if' statement | ||
; do something | ; do something | ||
loop ; | loop ; this is similar to an 'endif' statement | ||
int iCount | int iCount | ||
set iCount to 0 | |||
while ( iCount < 5 ) | |||
while iCount < 5 | let iCount += 1 ; Note: in this example, the increment is before the print | ||
let iCount += 1 | MessageEx "%g" iCount | ||
loop | |||
loop | ; The above code will print the numbers 1, 2, 3, 4 and 5 as messages in game | ||
set iCount to 0 | set iCount to 0 | ||
while iCount < 5 | while ( iCount < 5 ) | ||
let iCount += 1 | let iCount += 1 | ||
if iCount == 2 | if iCount == 2 | ||
Line 40: | Line 33: | ||
break ; end the loop immediately | break ; end the loop immediately | ||
endif | endif | ||
MessageEx "%g" iCount | |||
loop | loop | ||
; The above code will print the numbers 1 and 3 as messages in game. The number 2 is skipped due to the continue, whilst numbers 4 and 5 are skipped because of the break. | |||
</pre> | </pre> | ||
==Notes== | |||
*The above examples demonstrate simple usage of the expressions, see external documentation for more complex techniques. | |||
==See Also== | ==See Also== | ||
Line 50: | Line 49: | ||
*[[Goto]] | *[[Goto]] | ||
*[[Let]] | *[[Let]] | ||
[[Category:Functions_(NVSE)]] | [[Category:Functions_(NVSE)]] | ||
[[Category:Commands]] | [[Category:Commands]] | ||
[[Category:Scripting]] | [[Category:Scripting]] |