Come up with the remaining loop , flow , and example on each one .
1.while loop
2.do while loop
3.infinite loop
#include <iostream>
using namespace std;
int main() {
cout << "While loop: sum all numbers from 1 till the sum is less than N" << endl;
int i, sum, n;
cout << "Enter N: ";
cin >> n;
sum = 0;
i = 1;
while (sum < n) {
sum += i;
cout << "i = " << i << " sum = " << sum << endl;
i++;
}
cout << "Done!" << endl;
cout << endl << "do while loop" << endl;
char ans;
do {
cout << "Do you want to see this message ones more? (Y/n) ";
cin >> ans;
} while (ans != 'n');
cout << endl << "WARNING! Infinit loop (to terminate press Ctrl-C)" << endl;
int x=1;
while (x) {
x = 1;
}
return 0;
}
Comments