Loops

Hey guys, I'm learning to code myself from an old C++ text book. I came across this in the book where it asks to solve it. I have literally tried to code this but have had no success in the past 10 hours or so spent on this. C++ is my first programming language so don't be too hard on me. The commented part is what its asking for. Two different ways to write loops for Loops A,B,C. any help would be MUCH appreciated.

#include <iostream>
using namespace std;

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

}
Last edited on
well it's basically asking asking you to use all 3 kinds of loops(for, while, do-while) for 3 problems as a practice exercise.
for the A) part:

the 'for' version is given.

the 'while' version is:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
int i=0;
while(i<5)
{
cout<<i<<endl;
i++;
}
}



the 'do-while'version:


1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>
using namespace std;
int main()
{
int i=0;
do
{
cout<<i<<endl;
i++;
}while(i<5);
}


two ways for A) problem.
i haven't tested this so check it. i am also new. just started. so hope this is right.

hope this will give you insight on how to tackle b) and c). you should try it again yourself.

and i am not exactly sure but for beginner problems like this, 'beginner section' could be more appropriate than general 'C++ programming section'.

hope this helps


Topic archived. No new replies allowed.