For loops are perfect for tasks like iterating over arrays, processing a fixed number of elements, or performing actions a specific number of times. For example, printing a multiplication table up to a given number:
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
While loops are better suited for tasks where the loop's continuation depends on user input or other dynamic conditions. For example, reading input until a specific value is entered:
while True:
user_input = input("Enter a number: ")
if user_input == "quit":
break
print(f"You entered: {user_input}")