0

I need to convert this for loop to a do while:

for (int i = 0; i <= input; i = i + 2)
{
    cout << name << " is number " << input << endl;
    total = total + i;
}
cout << endl;
cout << total << endl;

This is what I have so far:

do
{
    cout << name << " is number " << input << endl;
    i += 2;
    total += i;
} while (i <= input);
cout << endl;
cout << total << endl;

It doesn't give the same total value as the for loop. What am I doing wrong?

3
  • Did you initialize i correctly to zero before the loop? Are you sure that input can never by negative? Commented Apr 3, 2012 at 16:16
  • Yeah I already have all of the variables listed with a value of 0, except for the string name, so I know it can't be that. Commented Apr 3, 2012 at 16:16
  • How are you initializing i the second time around? Commented Apr 3, 2012 at 16:16

6 Answers 6

4

You have to add i to the total before incrementing it by 2

So the do..while loop should be like this:

do
{
    cout << name << " is number " << input << endl;
    total += i;
    i += 2;
} while (i <= input);
cout << endl;
cout << total << endl;
Sign up to request clarification or add additional context in comments.

2 Comments

Glad to be of help.Please mark the answer as accepted if you think it has solved your query.
It won't do the same if the i > input before entering the loop.
2

The main difference between for loop and do-while loop is the fact that:

  • For loop executes 0 or more times
  • But the do-while loop executes 1 or more times

Example:

int input = 100;

//Will never execute as i is bigger than 5
for (int i = input; i<5; ++i)
    cout << i;

//Will execute only one time as i < 5 is checked only
//after first execution
int i = input;
do
{
    cout << i;
} while(i < 5);

The way to correctly do you task is:

int i = 0;
//if used to prevent first execution
if (i <= input)
{
    do
    {
        cout << name << " is number " << input << endl;
        total = total + i;
        i = i + 2;
    } while(i <= input);
}

But for is better to rewrite for loop like

for(BEFORE_STATEMENT; FINISH_STATEMENT; ITERATE_STATEMENT)
{
    LOOP_CODE
}

as while loop, which will work the same

BEFORE_STATEMENT
while(FINISH_STATEMENT)
{
    LOOP_CODE
    ITERATE_STATEMENT
}

Comments

1

You just need to change

i += 2;
total += i;

to

total += i;
i += 2;

In your for loop:

total = total + i;  

i is equal to 0 at the first iteration. The way you were doing it in the do - while loop, i was set to 2 before the total addition.

1 Comment

Damn looks like responding first isn't a criteria for upvotes anymore.
0

If you haven't done so in a previous portion of the code, you need to initialize i in the do...while.

Also, in the do...while, change the order to have total incremented before i is incremented.

Comments

0

Your code is incorrect. Corect is

do
{
    cout << name << " is number " << input << endl;
    total += i;//swap these lines
    i += 2;//
} while (i <= input);
cout << endl;
cout << total << endl;

Comments

-1

a do while will be exectued at least one time, no mather what the value is i is. Also you should initialise your i.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.