It doesn't need a second while loop. It joins any number of chans in lots of 5 (or less for the last few).

/debug @debug and see what gets sent.

Obviously there will be a limit on the amount of channels you can join that is set by the server.