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?

//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 ?

The output for the second is actually this:

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--

void CountDown(int nValue)  
{   
  int num = nValue;

  while (num > 0)  
  {  
     cout << (num--) << endl;
  }  
}
//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 ?

//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.

This article has been dead for over six months. Start a new discussion instead.