Thread for loop (10 answers)
Opened by nano at 2015-04-03 23:46

hlubenow
 2015-04-04 15:38
#180581 #180581
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Auch kein Problem.
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/perl

use warnings;
use strict;

print "please enter two odd numbers \n";
print "enter first odd number: ";
my $odd1 = <STDIN>;
chomp ($odd1);
print "enter second odd number: ";
my $odd2 = <STDIN>;
chomp ($odd2);

if ($odd1 % 2 == 0 || $odd2 % 2 == 0) {
    exit;
}
my @odds;
my $i;
my $step = 2;

if ($odd1 <= $odd2) {
    for ($i = $odd1; $i <= $odd2;$i += $step) {
        push(@odds, $i);
    }
} else {
    for ($i = $odd1; $i >= $odd2;$i -= $step) {
        push(@odds, $i);
    }
}

print "numbers: ";
foreach $i (@odds) {
    print "$i ";
}
print "\n";

Kann sogar sein, daß er die gerade die zwei verschiedenen for-Schleifen (im C-Stil) lernen sollte.

View full thread for loop