Solved

Needs help in do while

Posted on 2011-02-22
10
309 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 35

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

773 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