Question
Use c++ The code below implements a for loop, a while loop, and a do while loop. Your job is to update the provided code
Use c++
The code below implements a for loop, a while loop, and a do while loop. Your job is to update the provided code with alternative versions of the loop indicated in the comments. Note, when coding a for loop, you must put the update, test, and initialization in the designated spots in the for the statement.
In the process of converting, you might change what the initialization value is or what the termination condition is. Focus on whether the output of the new loop matches the output of the old loop rather than the initialization and termination condition of the original. Note: normally the update does not change.
#include
#include
using namespace std;
int main() {
int i = -55; // set to non intuitive value
cout << setfill('*') << setw(30) << "" << endl;
cout << "Loop A (while version)" << endl;
i = 0; // initialization
while (i < 10) { // termination condition
cout << "Enter a number less than 10 (Greater than 10 to exit):" << endl;
cin >> i; // update
cout << "Number: " << i << endl;
}
cout << setfill('*') << setw(30) << "" << endl;
cout << "Loop A (for version)" << endl;
// Add for loop code, don't forget initialization, termination condition and update
// Note: initialization, termination condition and update should
// all be in the () after the 'for' keyword!()
bool first = true;
for (i = 0; i<10; cin>>i) {
if (!first)
cout << "Number: " << i << endl;
else
first = false;
if (i<10)
cout << "Enter a number less than 10 (Greater than 10 to exit):" << endl;
}
cout << "Number: " << i << endl;
//***********************************************************************
cout << setfill('*') << setw(30) << "" << endl;
cout << "Loop B (for version)" << endl;
for (i = 0; i<5; i++) { //(initialization; termination condition; update)
cout << i << endl;
}
cout << setfill('*') << setw(30) << "" << endl;
cout << "Loop B (do while version)" << endl;
// Add do while loop code, don't forget initialization, termination condition and update
//***********************************************************************
cout << setfill('*') << setw(30) << "" << endl;
cout << "Loop C (do while version)" << endl;
i = 20; // initialization
do {
cout << i * 2 << endl; // output
i -= 2; // update
} while (i>10); // termination condition
cout << setfill('*') << setw(30) << "" << endl;
cout << "Loop C (for version)" << endl;
// Add while loop code, don't forget initialization, termination condition and update
cout << setfill('*') << setw(30) << "" << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started