Course of Raku / Essentials / Positional data types / Exercises / Print a series of numbers

Solution: Print a series of numbers

There is more than one way to solve the given problem.

Code 1

The first solution is more Raku-ish and compact.

my $begin = prompt 'Begin: ';
my $end = prompt 'End: ';

.say for $begin .. $end;

🦋 Find the program in the file series-of-numbers.raku.

Code 2

The second possible solution can use the loop statement.

my $begin = prompt 'Begin: ';
my $end = prompt 'End: ';

loop (my $n = $begin; $n <= $end; $n++) {
    say $n;
}

🦋 Find the program in the file series-of-numbers-loop.raku.

As you can see, the first variant is significantly shorter and more expressive.

Discussion

In the first variant of the program, the postfix form of the for loop is used. In the second program, a loop is chosen.

Output

Pass the two numbers in the console and run the program. Both variants produce the same output.

$ raku exercises/positionals/series-of-numbers.raku
Begin: 15
End: 19
15
16
17
18
19

Next exercise

💪 Spell a number

Course navigation

Typed variables / Allomorphs   |   Associative data types

Translations of this page: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська