Course of Raku / Advanced /
Control flow / given and when
/ Exercises / Even, odd, or
zero
Solution: Even, odd, or zero
Here is a possible solution to the task.
Code
my $n = 12;
given $n {
when 0 { say 'zero' }
when $_ %% 2 { say 'even' }
default { say 'odd' }
}🦋 You can find the source code in the file sign-of-a-number.raku.
Output
evenComments
when 0matches the single value zero. It comes first because zero is also even, and we want to report it on its own.when $_ %% 2is a condition, where$_is the topic set bygiven: the%%operator tests whether it divides evenly by two. The value12is not zero but is divisible by two, so the program printseven.defaultcovers everything left over — the numbers that are neither zero nor even, that is, the odd ones.