How to make a for loop into a do/while loop

I know in the for loop you have inttialization, condition and expression

do you just bring those things inside the body? any example would be much appreciated
Quick example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
    for(int i = 0; i < 10; i++)
    {
        cout << "i is now " << i << endl;
    }

    int x = 0;

    while(x < 10)
    {
        cout << "x is now " << x << endl;
        x++;
    }

    return 0;
}

The differences being the initialization needs to happen before and outside the while loop, otherwise you'll have an issue with variables being out of scope, and the expression will be the final statement (or statements).
or:

1
2
3
4
5
6
int x=0;

do{
        cout << "x is now " << x << endl;
        x++;
} while (x != 10);
thanks so much
for (guessNum = 0; guess != numPicked; guessNum++)
{
cout << " What would you like to guess? \n";
cin >> guess;
if(guess < numPicked)
cout << "\nYou guessed too low! \n \n";
else if (guess > numPicked)
cout << "\nYou guessed too high! \n \n";
}

how do i put that into do/while statement
1
2
3
4
5
6
7
8
9
10
11
12
guessNum = 0;

do
{
     cout << " What would you like to guess? \n";
     cin >> guess;
     if(guess < numPicked)
          cout << "\nYou guessed too low! \n \n";
     else if (guess > numPicked)
          cout << "\nYou guessed too high! \n \n";
     guessNum++;
}while (guess != numPicked);


All I did was
1. put the initialization before, and outside the loop
2. moved the condition to a while loop at the moved
3. Add the expression as the final statement of the loop
Topic archived. No new replies allowed.