A nested loop is constructed to print prime numbers from 1 to 20. Each number is tested for divisibility by numbers smaller than itself to identify primes, using commands like continue, break, and the modulo (%) operator. Boundary cases like division by zero must also be checked.