but that's better done in a loop that starts with 1000 and divides by 10 until less than 10.
I would disagree here. The loop approach is harder to implement and will consume a lot more memory because the Arduino lacks hardware division. The approach with the if statements is much simpler and compiles to less instructions.
Even in case of variable digits I would stick with the if statements like so:
switch (digits) {
case 5: { if (n<10000) { print('0'); }}
case 4: { if (n<1000) { print('0'); }}
case 3: { if (n<100) { print('0'); }}
case 2: { if (n<10) { print('0'); }}
}
print(n);