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 ?