•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,411 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,017 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 294 | Replies: 5
![]() |
•
•
Join Date: Nov 2007
Posts: 46
Reputation:
Rep Power: 1
Solved Threads: 2
I have two programs both do the same thing but both when run gave totally different results. I am sure there is a lesson to learn from this and that will teach you a fundamental difference between the ‘if’ and the ‘while’. Can anyone explain to me why both the programs ran differently as I am only a novice?
Result when run:
10
9
8
7
6
5
4
3
2
1
0
Result when run:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0^C
Weird isn't it ?
//Using if
#include <iostream>
using namespace std;
void CountDown(int nValue)
{ cout << nValue << endl;
if (nValue > 0)
{
CountDown((nValue-1));}
}
void main(void)
{
CountDown(10);
} Result when run:
10
9
8
7
6
5
4
3
2
1
0
//Using while
#include <iostream>
using namespace std;
void CountDown(int nValue)
{ cout << nValue << endl;
while (nValue > 0)
{
CountDown((nValue-1));}
}
void main(void)
{
CountDown(10);
} Result when run:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0^C
Weird isn't it ?
•
•
Join Date: Jan 2008
Posts: 1,738
Reputation:
Rep Power: 8
Solved Threads: 215
The output for the second is actually this:
You can't see the first ten numbers since they go by too fast. Try putting a pause in there. Hit the enter key repeatedly.
You're in an infinite while loop. nValue never decreases so if you get into that while loop you will never leave.
10 9 8 7 6 5 4 3 2 1 0 0 0 etc...
You can't see the first ten numbers since they go by too fast. Try putting a pause in there. Hit the enter key repeatedly.
//Using while
#include <iostream>
using namespace std;
void CountDown(int nValue)
{
cout << nValue << endl;
while (nValue > 0)
{
cin.get();
CountDown((nValue-1));
}
}
int main()
{
CountDown(10);
return 0;
} You're in an infinite while loop. nValue never decreases so if you get into that while loop you will never leave.
Your while statement is a recursively infinite loop.
10 will always be greater than 0, so will 9 then 8 then 7 then 6 then 5 then 4 then 3 then 2 then 1 but not zero.
Because of this, the statement (while 1 > 0) keeps printing out CountDown(0) and therefore you see 0's on the screen.
You may not be seeing the other values 10-0 because they happen so fast since your program gets flooded with zeros.
You may want to use either a reference to an int or a locally scoped int that takes the value of the argument and then change the local int as the while loop iterates--
10 will always be greater than 0, so will 9 then 8 then 7 then 6 then 5 then 4 then 3 then 2 then 1 but not zero.
Because of this, the statement (while 1 > 0) keeps printing out CountDown(0) and therefore you see 0's on the screen.
You may not be seeing the other values 10-0 because they happen so fast since your program gets flooded with zeros.
You may want to use either a reference to an int or a locally scoped int that takes the value of the argument and then change the local int as the while loop iterates--
c++ Syntax (Toggle Plain Text)
void CountDown(int nValue) { int num = nValue; while (num > 0) { cout << (num--) << endl; } }
Last edited by Alex Edwards : Jun 29th, 2008 at 3:11 pm.
•
•
Join Date: Nov 2007
Posts: 46
Reputation:
Rep Power: 1
Solved Threads: 2
//Using while
#include <iostream>
using namespace std;
void CountDown(int nValue)
{
cout << nValue << endl;
while (nValue != 0)
{
cin.get();
CountDown((nValue-1));
}
}
int main()
{
CountDown(10);
return 0;
}this is should work ? but why isn't this too ?
•
•
Join Date: Jan 2008
Posts: 1,738
Reputation:
Rep Power: 8
Solved Threads: 215
•
•
•
•
//Using while #include <iostream> using namespace std; void CountDown(int nValue) { cout << nValue << endl; while (nValue != 0) { cin.get(); CountDown((nValue-1)); } } int main() { CountDown(10); return 0; }
this is should work ? but why isn't this too ?
It "works" in that it allows you to see the numbers. They don't go by so fast that you can't see them. Hit the "Enter" key and another number appears so you can see what is going on. One number appears every time you hit enter. It's your code, just with the
cin.get() added. It doesn't "work" in that you still have your infinite while loop, so it goes on forever. Try replacing the function with the one Alex posted to see how a while loop counts down. Generally you don't mix a while loop with recursion in a situation like yours. ![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Hardware Interrupts & 100% CPU usage (Windows NT / 2000 / XP / 2003)
- Chargeback Software & Overseas Customers (eCommerce)
- ASP.net/Stored Proc & Login Verification (ASP.NET)
- Trademark & libel issues in online communities (Growing an Online Community)
- Greeting Your Members & Guests (PHP)
- Can Someone please check my project & comment/tips (C)
- Buying & Selling Google PageRank (Website Reviews)
- Click.linksynergy & donet detect(), HELP ME GET RID OF THIS .........PLEASE (Viruses, Spyware and other Nasties)
- Page Cannot Be Displayed & javascript:doNetDetect() Errors (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Cannot use ifstream and ofstream in Startup program
- Next Thread: exit button



Linear Mode