The while loop can be used when you know how many times something will happen, just like the for loop. Figure 4.6 shows how a while loop can work much like a for loop: Figure 4.6: Although the output of this program looks a lot like the basic for loop, it uses a different construct to achieve the same result.