954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Question about do-while loop

I am a puzzled regarding the purpose of the countDown = coundDown -1; part of this program code. I mean I do not know why I should subtract the countDown by 1. What is it for??? Here is the program source code, the one highlighted is the one I'm confused with.


1 #include
2 using namespace std;
3 int main( )
4 {
5 int countDown;
6 cout << "How many greetings do you want? ";
7 cin >> countDown;
8 do
9 {
10 cout << "Hello ";
11 countDown = countDown - 1;
12 }while (countDown > 0);
13 cout << endl;
14 cout << "That’s all!\n";
15 return 0;
16 }

francis25
Newbie Poster
18 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

countDown is used to limit the number of times the loop runs. Notice the while statement condition right after that line. If you never subtracted from countDown, that statement would always be true (unless the user entered a negative number in line 7) and the loop would run forever.

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 
countDown is used to limit the number of times the loop runs. Notice the while statement condition right after that line. If you never subtracted from countDown, that statement would always be true (unless the user entered a negative number in line 7) and the loop would run forever.


It's a loop; it iterates(keeps going)until it reaches a desired goal. By using a variable name "countDown" = "countDown" - 1 explicity gives information to decrement the current value of the variable "countDown" by 1 minus its self, which is the current value the variable is holding.

Loops iterate, and increment and decrement methods, such as variable = variable - 1 or variable++(for increment, adding)or variable--(for decrement, subtracting).

The point of iterating(to keep going)is to keep changing the variables' values until they reach a certain point in which the loop will end or you can break from it and the result will have been completed(the result being whatever you wish it to be). This, of course, applying to a loop with a certain value that must be reached and not other tasks that can be done in a loop.

Hope I was some what useful.

spoonlicker
Junior Poster
127 posts since Feb 2011
Reputation Points: -9
Solved Threads: 3
 

This is code for the game Yahtzee but it shows a good do-while loop. Well, I think anyway.

do
	{
	    cout << "Roll Again?? (y/n) ";
	    cin >> again;
		cout << endl;
		if (again != 'y')
	    {
			cout << "Next player's turn!!" << endl;
			cout << "_______________________________________________________________________" << endl;
			cout << endl;
		}
		else if (again == 'y')
		{
		    cout << "How Many Die do you want to Roll Again? (1-5) ";
		    cin >> roll;
			cout << endl;
		    for (int i = 0; i < roll; ++i) // Tells the player his results from his dice roll
			{
				iRoll = (rand() % 6);
				cout << "You rolled a " << Roll[iRoll] << endl; // Tells him what he got on the 1st roll
				rolldie.push_back(roll);
			}	
			cout << endl;
		}
	}while (again == 'y');
Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
 

I am building the game as we speak.

Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
 

This is code for the game Yahtzee but it shows a good do-while loop. Well, I think anyway.

do
	{
	    cout << "Roll Again?? (y/n) ";
	    cin >> again;
		cout << endl;
		if (again != 'y')
	    {
			cout << "Next player's turn!!" << endl;
			cout << "_______________________________________________________________________" << endl;
			cout << endl;
		}
		else if (again == 'y')
		{
		    cout << "How Many Die do you want to Roll Again? (1-5) ";
		    cin >> roll;
			cout << endl;
		    for (int i = 0; i < roll; ++i) // Tells the player his results from his dice roll
			{
				iRoll = (rand() % 6);
				cout << "You rolled a " << Roll[iRoll] << endl; // Tells him what he got on the 1st roll
				rolldie.push_back(roll);
			}	
			cout << endl;
		}
	}while (again == 'y');

That's a...um...pretty bad example there. Mine was much better.

spoonlicker
Junior Poster
127 posts since Feb 2011
Reputation Points: -9
Solved Threads: 3
 

How come this doesn't work for my assignment? This is my only flaw. 2010 says so.

cout << "What numbers do you want to keep on this roll? ";
	cin >> number;

	for ( int i = 0; i < 7; i++) // Yahtzee... (-_-")
	{
		cout << rolldie.push_back(number);
	}
Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
 

What's your example??

Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
 
What's your example??

Sorry, it's in another thread. Click my profile and you can see it in my previous threads and compare it if you'd like.

spoonlicker
Junior Poster
127 posts since Feb 2011
Reputation Points: -9
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: