Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

About WHILE loop.

+5 votes
Can the WHILE loop completely replace the FOR loop?
asked Oct 6, 2016 in CSC 211 (F16) by Trung Le (Jayden) (100 points)

2 Answers

+2 votes
I think they serve two different purposes, so they will always both be wanted. A "for" loop is good for when you know how many times you want it to loop, and a "while" loop is good for when you may not necessarily know how many times the loop should run. Our book said while loops come up in "interactive programs and file processing" for example.
answered Oct 6, 2016 by Brent Pierce (100 points)
+2 votes
Well each loop has its own advantages, and you should use them wisely to simplify your code.

FOR loop is useful because you can easily determine how many times you want your code run.

WHILE loop is useful because you can let your program run whatever times you don't need to care but to fulfill your requirements.
answered Oct 6, 2016 by Minh Ta (100 points)
and yes, you can completely replace FOR with WHILE, but it is not really recommended.
ai cho may tra loi cau hoi cua tao
...