#! /usr/bin/perl

### foreach loop

@nums = (1,2,3,4,3,4,5,6);
$cnt4 = 0;

foreach $num ( @nums ) {
   if ($num == 4) {
      $cnt4++;
   }
}
print "Found $cnt4 4's in the array\n";
$ans = <>;

$check = 5;
foreach $num ( @nums ) {
    print "$num\n";
   if ($num == $check) {
      print "The array contains at least one $check\n";
      last;
   }
}
$ans = <>;

foreach $num (1..10) {
   print "$num ";
}
print "\n";
$ans = <>;


### for loops
$N = 100;

$sum = 0;
for ( $i = 1; $i <= $N; $i++) {
   $sum += $i;
}
print "The sum of all integers 1..$N is $sum\n";
$ans = <>;

$sum = 0;
for ( $i = 1; $i <= $N; $i += 2) {
   $sum += $i;
}
print "The sum of all odd integers 1..$N is $sum\n";
$ans = <>;

$sum = 0;
for ( $i = 1; $i <= $N; $i++) {
   next if ($i % 2 == 0);
   $sum += $i;
}
print "The sum of all odd integers 1..$N is $sum\n";
$ans = <>;

$sum = 0;
for ( $i = $N; $i >= 1; $i--) {
   $sum += $i;
}
print "The sum of all integers 1..$N is $sum\n";
$ans = <>;

## While loops

$sum = 0;
$i = 1;
while ($i <= $N ) {
   $sum += $i; 
   $i++;
}
print "The sum of all integers 1..$N is $sum\n";
$ans = <>;

@arr = (1,3,4,0,2,3);
while ( @arr ) {
   print shift @arr, "\n";
}
$ans = <>;

### Until loops

$sum = 0;
$i = 1;
until ($i > $N ) {
   $sum += $i; 
   $i++;
}
print "The sum of all integers 1..$N is $sum\n";
$ans = <>;

