Answer:
- You know how many times you want the loop to run.
- When there is a defined start and end.
Explanation:
Required:
When to use for instead of while loop?
First, it should be noted that both for loop and while loop are used for iteration. However, depending on the scenario; there is preference on one to the other.
The basic syntax of for loop is:
[tex]for(start;end;condition)\{\}[/tex]
The condition could either be increment or decrement
The basic syntax of while loop is:
[tex]while(condition)\{\}[/tex]
The condition could be any condition whatsoever
Analyzing the options one after the other.
1. How many times the loop to run.
The for loop is preferred here because of the simplicity of the for loop.
2. & 3. Both the for and while loops are used for code repetition and to perform number calculations
4. Defined start and end.
From the syntax of the for loop
[tex]for(start;end;condition)\{\}[/tex]
A for loop has a definite start and end in its basic syntax and is more preferable to use instead of a while loop.
Hence, 1 & 4 answer the question