A while loop is typically used when the number of iterations is unknown or depends on a condition that may change during execution. In contrast, a for loop is usually preferred when the number of iterations is known in advance, such as when iterating through a fixed range. A while loop continues executing as long as its condition remains true, making it ideal for situations where you are waiting for some event to occur or processing input that may vary in length or content.