Hamilton LaboratoriesHamilton C shell 2012User guideTutorials

foreach, for, while and repeat

Oregon Coast

foreach, for, while and repeat
Previous | Next

Topics

foreach statement
for statement
while statement
repeat statement

The foreach, for and repeat statements provide for iteration.

foreach statement

The foreach statement is designed for iterating over a series of words. In this example, i is iterated over the list of all the files in the Samples directory. Each one, in turn, is tested to see if it’s executable (i.e., has a .csh, .cmd, .bat, .exe or .com extension or is a valid binary executable.)

410 D% cd ~\samples 411 D% ls args.c dumpenv.c finance.csh myecho.exe readme args.exe dumpenv.exe makecpgm.csh rcode.c bits.csh factor.csh myecho.c rcode.exe 412 D% foreach i (*) 413 D? if (-x $i) echo $i is executable 414 D? end args.exe is executable bits.csh is executable dumpenv.exe is executable factor.csh is executable finance.csh is executable makecpgm.csh is executable myecho.exe is executable rcode.exe is executable

for statement

The for statement provides more traditional iteration over numerical values. If you specify a range (e.g., 1 to 3) but don’t specify the increment, 1 is assumed. Although this example shows iteration over integer values, floating point values are equally acceptable.

415 D% for i = 1 to 3 do 416 D? echo $i 417 D? end 1 2 3

You can also iterate over a list of ranges or individual values. The to and by clauses may be specified in either order.

418 D% for i = 1, 4, 7, 12, -4 to 6 by 3 do 419 D? echo $i 420 D? end 1 4 7 12 -4 -1 2 5

while statement

The while statement works in the traditional manner, iterating so long as the while condition is true. This example keeps popping up through the various levels of parent directories until it reaches the root. fullpath is one of the builtin procedures; it return the fully-qualified pathname of its argument. Notice that fullpath is invoked in three different ways: on line 421, as if it were a command, on 422 in more conventional procedure syntax and on 423, where it’s substituted in as if it were a variable.

421 D% fullpath . d:\Nicki\samples 422 D% while (fullpath(".") !~ "[a-zA-Z]:\") 423 D? echo $fullpath(".") 424 D? cd .. 425 D? end d:\Nicki\samples d:\Nicki 426 D% cd d:\

repeat statement

The repeat statement has two forms. In the short form, a numeric constant (not an expression) specifies the number of times to execute the statement following on the same line.

427 D% repeat 4 echo do this again do this again do this again do this again do this again

In the long form, repeat provides the more conventional repeat structure, iterating until some exit condition satisfied.

428 D% calc i = 1 1 429 D% repeat 430 D? calc i++ 431 D? until (i > 5) 1 2 3 4 5

See also

foreach, for, while and repeat
Miscellaneous statements
Expression operators
Wildcard characters
Tutorial: Expressions
Tutorial: Wildcarding

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.