## WeBWorK Problems

### If / else loops and wanting to skip numbers

Number of replies: 5
If I wanted to have a "if/else" loop and I wanted it to count the numbers as "if i = 0, 3, 6, 9, .., 21" how would I code it? Would it be:

if ($i % 3 == 0) {do this;} else {do that;} } Does that help? If you only want to take action on multiples of 3, you could generate the array (0,3,...,21) and do like this: for$i map{3*$_} (0..7) { do this; } The part that goes "map{3*$_} (0..7)" is taking the array 0 through 7, multiplying everything by 3, and leaving you with (0, 3, 6, 9, 12, 15, 18, 21). Then $i only iterates through those values. In reply to Alex Jordan ### Re: If / else loops and wanting to skip numbers by Teresa Adams - Thanks, that helps a lot. If I used: for$i (0..21) {
if ($i % 3 == 0) {do this;} elsif ($ % 3 ==1 {do that;}
else {do that;}
}

would the the "elsif" work for i=1, 4, 7, etc?

and else work for i = 2, 5, 8, 11?

Thanks,
Teresa

### Re: If / else loops and wanting to skip numbers

by Alex Jordan -
Yes, that should work except for some typos. ("$" instead of "$i" and a missing close paren.)

### Re: If / else loops and wanting to skip numbers

for $i (0..7) { do stuff to 3*$i;
do stuff to 3*$i+1; do stuff to 3*$i+2;