for
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
| — | for [2006/08/01 03:13] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ======Synopsis: | ||
| + | [[for]] (//once//, // | ||
| + | [[for]] //var// from // | ||
| + | [[for]] //var// from // | ||
| + | [[for]] //var// in (//word list//) { // | ||
| + | |||
| + | ======Description (standard form): | ||
| + | [[FOR]] is a general purpose loop. It is modeled on the C for statement, | ||
| + | and works in a very similar manner. | ||
| + | three parts to a [[FOR]] loop: | ||
| + | |||
| + | The //once// part is a // | ||
| + | iterating. | ||
| + | variables that will be used in the loop. | ||
| + | |||
| + | Before each loop iteration, the // | ||
| + | Most often, this is used to see if the counter has exceeded a certain limit. | ||
| + | The condition may contain any expression legal in the [[IF]] command. | ||
| + | Because of this, the loop does not necessarily have to iterate at all. | ||
| + | |||
| + | The //each// part is a // | ||
| + | before the // | ||
| + | counter that gets checked by the condition statement. | ||
| + | |||
| + | It is imperative to note that the three sections are separated by commas | ||
| + | and not semicolons; this is because //once// and //each// may contain | ||
| + | semicolons if they contain multiple statements. | ||
| + | The first and third section are **COMMANDS** and the second section is an | ||
| + | **EXPRESSION**. | ||
| + | |||
| + | //Once//, //each//, and //block// are optional and may be left blank. | ||
| + | |||
| + | ======Description (for/next form): | ||
| + | This second form of the command provides a FOR .. NEXT type loop that | ||
| + | enumerates each of the integers between // | ||
| + | Each enumerated integer is assigned to //var// and the //block// is | ||
| + | executed. | ||
| + | |||
| + | If you wish enumerate only some of the values in the range, you can | ||
| + | specify a //step//, which says that each //step//th value should be | ||
| + | enumerated. | ||
| + | |||
| + | If // | ||
| + | //step// value and it must be negative. | ||
| + | the loop will not execute. | ||
| + | |||
| + | The //var// variable should be considered read-only. | ||
| + | **not** affect how the range is enumerated. | ||
| + | |||
| + | This form is much cheaper than a standard [[for]], and is cheaper than a [[fe]] | ||
| + | over a [[jot]] range. | ||
| + | |||
| + | The [[CONTINUE]] and [[BREAK]] commands are honored in the action body. | ||
| + | The [[CONTINUE]] command will behave as a " | ||
| + | command will abort the loop entirely. | ||
| + | |||
| + | ======Description (for/each form): | ||
| + | This third and final form is a FOR .. IN LIST type loop that iterates over | ||
| + | each word in the word list, assiging each value in turn to the //var// and | ||
| + | executing the //block//. | ||
| + | |||
| + | This form is cheaper than [[fe]] because it is optimized for iterating | ||
| + | one word at a time, whereas [[fe]] is general-purpose. | ||
| + | |||
| + | The [[CONTINUE]] and [[BREAK]] commands are also honored in the same way | ||
| + | as in the second form above. | ||
| + | |||
| + | ======Examples: | ||
| + | Form 1: | ||
| + | |||
| + | To display a warning message 3 times: | ||
| + | for ( @ xx = 3, xx > 0, @ xx-- ) { | ||
| + | echo WARNING! | ||
| + | } | ||
| + | |||
| + | A infinite loop that behaves like the Unix ' | ||
| + | for ( ,, ) { | ||
| + | echo yes | ||
| + | } | ||
| + | |||
| + | Form 2: | ||
| + | |||
| + | Display the numbers 1 through 5 each on a separate line: | ||
| + | for ii from 1 to 5 { | ||
| + | echo $ii | ||
| + | } | ||
| + | |||
| + | Display the numbers 1, 3, and 5 each on a separate line: | ||
| + | for ii from 1 to 5 step 2 { | ||
| + | echo $ii | ||
| + | } | ||
| + | |||
| + | Form 3: | ||
| + | |||
| + | Display the words " | ||
| + | for xx in (one two three) { | ||
| + | echo $xx | ||
| + | } | ||
| + | |||
| + | ======History: | ||
| + | |||
for.txt · Last modified: 2006/08/01 03:13 by 127.0.0.1
