Solved

Needs help in do while

Posted on 2011-02-22
10
285 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 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 35

Expert Comment

by:mccarl
ID: 34957609
asterisks variable is not reset between inner loop and outer loop
0
 
LVL 8

Expert Comment

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

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 35

Expert Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now