Solved

Needs help in do while

Posted on 2011-02-22
10
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 100 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 100 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
Independent Software Vendors: 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 100 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 100 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 100 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Part One of the two-part Q&A series with MalwareTech.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

630 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