InterviewSolution
Saved Bookmarks
| 1. |
What is the difference between while and for loop? When should these loops be used? |
|
Answer» The body ofloopcan either be empty or a singlestatementor a block of statements. The condition may be any expression, and true is any non-zero value. Theloopiterateswhilethe condition is true. When the condition becomes false, program control passes to the line of code immediately following theloop.In general, youshoulduse a forloopwhen you know how many times theloop shouldrun. If you want theloopto break based on a condition other than the number of times it runs, youshoulduse a whileloop. |
|