Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Needs help in do while

Posted on 2011-02-22
10
Medium Priority
?
364 Views
Last Modified: 2012-05-11
Hi guys: I just change the for statement into do while but the answer is not same. Can any one please help me out ? Thanks.

#include <iostream>
using namespace std;

int main()
{
      int asterisks = 1;
      int row = 1;
      do
      {
            row+=1;
            do
            {
                  asterisks +=1;
                  cout << '*';
                  cout << endl;
            } while (asterisks <= (8-row));

      } while( row < 8);
      asterisks = 1;
      system("pause");
      return 0;
}

//      for (int row = 1; row < 8; row +=1)
//      {
//            for(int asterisks = 1; asterisks <= (8-row) ; asterisks +=1)
//                  cout << '*';
//      cout << endl;
//      }
0
Comment
Question by:mustish1
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 400 total points
ID: 34957602
The difference between a for loop and a do while loop is this:

In a for loop, the termination test is done BEFORE the loop body is executed - even the first time.  If the test fails the first time, the loop body is not executed at all.

In a do/while loop, the loop body is executed first and then the test is made.  This ensures that the loop body will always be executed AT LEAST ONCE.

Depending on the logic you desire, sometimes for is the right loop, sometimes do/while.  

However do/while is much rarer and most programs do not use it.  Usually a for loop can be written that is equivalent to the do/while
0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 400 total points
ID: 34957604
Either initialize asterisks and row to 0, or move the row += 1 and asterisks += 1 lines to the end of the do-while block, just before the corresponding while statement. I think either of those should do it.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 34957609
asterisks variable is not reset between inner loop and outer loop
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:crysallus
ID: 34957615
...oops, and yeah, what mccarl said.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
ID: 34957617
This should be equivalent:
int asterisks = 1;
int row = 1;

do
{ 
    do
    {
        cout << '*';
        cout << endl;
        asterisks +=1;
    } while (asterisks <= (8-row));

    row+=1;
} while( row < 8);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34957620
Scratch that. Seems mccarl is on his game tonight  = )
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 400 total points
ID: 34957634
Also move cout << endl; out of the inner loop:
int asterisks = 1;
int row = 1;
do
{ 
    do
    {
        cout << '*';
        asterisks +=1;
    } while (asterisks <= (8-row));
        cout << endl;
    row+=1;
} while( row < 8);

Open in new window

0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 400 total points
ID: 34957647
There are two major problems in your uncommented code:
1) "do...while" loop will perform at least once before the condition is unsatisfied.
2) You did not re-initialize the asterisks variable for each outer loop.

That means in your uncommented code:
      do
      { 
            row+=1;                      // line 1
            do
            {
                  asterisks +=1;           // line 2
                  cout << '*';
                  cout << endl;           // line 3
            } while (asterisks <= (8-row));

      } while( row < 8);

Open in new window

     
The loops will perform like this:
Outer Loop 1:  
At line 1: row = 2
At line 2: asterisks = 2 => 3 => 4 => 5 => 6
=> print 5 stars in 5 separate lines

Outer loop 2:
At line 1: row = 3
At line 2: asterisks = 7
=> print 1 star in a separate line

Outer loop 3:
At line 1: row = 4
At line 2: asterisks = 8
=> print 1 star in a separate line

Outer loop 4:
At line 1: row = 5
At line 2: asterisks = 9
=> print 1 star in a separate line

Outer loop 5:
At line 1: row = 6
At line 2: asterisks = 10
=> print 1 star in a separate line

Outer loop 6:
At line 1: row = 7
At line 2: asterisks = 11
=> print 1 star in a separate line

To solve this problem, you should consider re-arranging the line 1, 2 and 3 in the above code like this:

#include <iostream>
using namespace std;

int main()
{
      int asterisks = 1;
      int row = 1;
      do
      { 
	    asterisks = 1;
            do
            {
                  cout << '*';
                  asterisks +=1;
            } while (asterisks <= (8-row));

            cout << endl;
            row+=1;
      } while( row < 8);
      system("pause");
      return 0;
}

Open in new window

0
 

Author Closing Comment

by:mustish1
ID: 34957665
Very detailed help. Thanks a lot to every one.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 34957696
Hmmmm, ok, ????
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question