Foreach

Updated: 12/16/2018 by Computer Hope

Foreach may refer to any of the following:

foreach

1. Foreach is a loop statement in programming that performs predefined tasks while or until a predetermined condition is met. For example, in Perl you can use a foreach loop that prints all the elements of an array one by one. Sample code:

Foreach on array in Perl

foreach $value (@value) {
 print "$value\n";
}

If the elements of the array was "one," "two," and "three" each would be printed on a new line by itself.

Foreach on hash in Perl

In the next example, the foreach would step through a hash displaying the hash and the key value.

foreach $example (keys %value) {
 print "$example = $value{$example}";
}

2. Foreach is also a Linux command, see the foreach command page for further information.

Do, For, Loop, Programming terms, While