Explain the difference between 'for' and 'while' statements with examples?
Answers
Answer:
While both for and while are entry-control loops used to execute block(s) of code repeatedly certain number of times, they differ in functionality. The for loop is quite similar to the while loop in terms of memory consumption and speed. However, the for loop is preferable when you know exactly the number of times the loop has to be repeated. On the contrary, while loop is appropriate when the exact number of iterations is not known, meaning you do not know how many times the loop has to be repeated.
Answer:
The 'for' loop used only when we already knew the number of iterations. The 'while' loop used only when the number of iteration are not exactly known. If the condition is not put up in 'for' loop, then loop iterates infinite times. ... In 'while' loop, the iteration statement can be written anywhere in the loop.
Explanation:
i hope it is helpful
please thanks my all answers and follow me