- #1
carl123
- 56
- 0
a) The following code implements a for loop, a do while loop, and a while loop. There are stubs for four additional loops. You are to add the code necessary to implement the missing loops as indicated by the comments. When adding code for a for loop, you must fill in all three parts in the for statement, i.e. the parts inside the parenthesis. Outputs for the three versions of Loop A should be identical. The three versions of Loop B should behave in the same way. Outputs for the three versions of Loop C should be identical.
b) As a comment in the code above describe the scope of the variable i inside and outside the body of each version of each loop.
c) What is the minimum number of iterations for each type of loop? You can put this as a comment at the top of your code.
Code:
#include "std_lib_facilities_4.h"
int main(int argc, char* argv[]) {
cout << "Loop A (for version)" << endl;
for (int i=0;i<5;i++) {
cout << i << endl;
}
cout << "Loop A (while version)" << endl;
// Add while loop code
cout << "Loop A (do while version)" << endl;
// Add do while loop code
cout << "Loop B (do while version)" << endl;
int i;
do {
cout << "Enter a number less than 100 (Greater than 100 to exit):" << endl;
cin >> i;
cout << i << endl;
} while (i<100);
cout << "Loop B (for version)" << endl;
// Add for loop code
cout << "Loop B (while version)" << endl;
// Add while loop code
cout << "Loop C (while version)" << endl;
int i = 20;
while (i>10) {
cout << i*2 << " ";
i-=2;
}
cout << endl << endl;
cout << "Loop C (do while version)" << endl;
// Add do while loop code
cout << "Loop C (for version)" << endl;
// Add for loop code
}
b) As a comment in the code above describe the scope of the variable i inside and outside the body of each version of each loop.
c) What is the minimum number of iterations for each type of loop? You can put this as a comment at the top of your code.