In: Computer Science
in c++ >>When is beneficial to use entry condition loop (for loop) and when is it beneficial to use exit condition loop (while loop). Give an example for each loops.
`Hey,
Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
for loop is used when we know how much time we have to run the iterations for example sum of first 10 numbers. While loop is used when we know the exit condition which means calculating the sum until the sum reaches a tolerance level.
For example
#include <iostream>
using namespace std;
int main()
{
int i = 0;
for (i = 5; i < 10; i++) {
cout << "hello\n";
}
return 0;
}
While loop
#include <iostream>
using namespace std;
int main()
{
int i = 5;
while (i < 10) {
i++;
cout << "GFG\n";
}
return 0;
}
Kindly revert for any queries
Thanks.