Instead of doing the number of channels + 1 thing, you can just notice after the while, like this:
while $chan(%x) { var %y = %y $v1 inc %x } .notice $nick I\'m on these channels: %y Total: $chan(0)