When to use while
Use the while loop when you do not know in advance how many times the loop is to execute.
A useful example is when you get information from the user. For example, you want the user to give you a ‘y’ for yes, or a ‘n’ for no. Try this:
char choice;
cout << “enter your choice” << endl;
cin >> choice;
while (choice == ‘y’) {
cout << “enter again, y or n: “ << endl;
cin >> choice;
}