Solved

Needs help in do while

Posted on 2011-02-22
10
297 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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 126
recursion example 16 111
Apps blocked by Java 9 63
T-SQL:  Sigh---Boy, this is fun.... 12 32
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

16 Experts available now in Live!

Get 1:1 Help Now