How similar and different while & for loops are? Give example

3.A) How similar and different while & for loops are? Give example

Answer:-

For loopWhile loop
The structure of for loop is –
for(initial condition; number of iterations){//body of the loop }
The structure of the while loop is-
While(condition){statements;//body}
Iterates for a preset number of times.Iterates till a condition is met.
In the absence of a condition, the loop iterates for an infinite number of times till it reaches the break command.In the absence of a condition, the while loop shows an error.
Initialization in for loop is done only once when the program starts.Initialization is done every time the loop is iterated.
Used to obtain the result only when the number of iterations is known.Used to satisfy the condition when the number of iterations is unknown.

One similarity between while and for loop is that they both are entry controlled loops i.e. they both will check the condition for true or false. If the condition is true then only the entry within the loop is permitted

Leave a Reply

Your email address will not be published. Required fields are marked *