Difference between revisions of "While"

444 bytes added ,  08:49, 28 August 2014
clarified + added external links
imported>Odessa
(Started page)
 
imported>Odessa
(clarified + added external links)
 
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 '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.
  |summary = Added by NVSE V4. Whilst a specified condition is met, the code section below, until the <b>loop</b> command, will repeat. Within while loops, the command <b>continue</b> may be used to skip remaining loop code and return to the while line. In the same context, the command <b>break</b> may be used to end the loop immediately, regardless of the while condition.


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.  
The same effect could be achieved using <b>label</b>, <b>goto</b>, <b>if</b> and <b>endif</b> 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 [[Causes of CTDs|crash the game]].  
  |name = While
  |name = while
  |returnType = void
  |returnType = void
  |arguments =  
  |arguments =  
   {{FunctionArgument
   {{FunctionArgument
   |Name = Condition
   |Name = Condition
   |Type = boolean
   |Type = expression
   }}
   }}
}}
}}
==Examples==
<pre>
<pre>
while ( SomeCondition ) ; this is similar to an 'if' statement
while SomeCondition ; * this is similar to an 'if' statement
     ; do something
     ; do something
loop ; this is similar to an 'endif' statement
loop ; * this is similar to an 'endif' statement
</pre>


The code below will [[Print|print]] the numbers 1, 2, 3, 4 and 5 to the console:
<pre>
int iCount
int iCount
set iCount to 0
let iCount := 0      ; * 'set iCount to 0...'
while ( iCount < 5 )
 
     let iCount += 1 ; Note: in this example, the increment is before the print
while iCount < 5     ; * 'if iCount is less than 5...'
     MessageEx "%g" iCount
     let iCount += 1 ; * 'add 1 to iCount...'
loop
     Print $iCount   ; * 'then print it to the console...'
; The above code will print the numbers 1, 2, 3, 4 and 5 as messages in game
loop                 ; * 'then go back to the while line.'
</pre>


The code below will print the numbers 1 and 3 to the console. The number 2 is skipped due to the <b>continue</b>, whilst numbers 4 and 5 are skipped because of the <b>break</b>.
<pre>
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 33: Line 40:
         break ; end the loop immediately
         break ; end the loop immediately
     endif
     endif
     MessageEx "%g" iCount
     Print $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 49: Line 50:
*[[Goto]]
*[[Goto]]
*[[Let]]
*[[Let]]
==External Links==
*[http://obse.silverlock.org/obse_command_doc.html#OBSE_Expressions List of supported expression found in OBSE, these are equivalent in NVSE]
*[http://fallout.bethsoft.com/eng/links/privacyredirect.php?site=http://www.loverslab.com/topic/26749-tutorial-nvse4-part-1-syntax-and-expressions/ Tutorial on syntax and expressions in NVSE 4]
[[Category:Functions_(NVSE)]]
[[Category:Functions_(NVSE)]]
[[Category:Commands]]
[[Category:Commands]]
[[Category:Scripting]]
[[Category:Scripting]]
Anonymous user