User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 46
Reputation: manzoor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
manzoor manzoor is offline Offline
Light Poster

while & if statement

  #1  
Jun 29th, 2008
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 ?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,613
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: while & if statement

  #2  
Jun 29th, 2008
The first part of the 2nd one probably flew right by you:
10
9
8
7
6
5
4
3
2
1
0
0
0
Reply With Quote  
Join Date: Jan 2008
Posts: 1,738
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 215
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: while & if statement

  #3  
Jun 29th, 2008
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.
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 774
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 76
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Master Poster

Re: while & if statement

  #4  
Jun 29th, 2008
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--

  1. void CountDown(int nValue)
  2. {
  3. int num = nValue;
  4.  
  5. while (num > 0)
  6. {
  7. cout << (num--) << endl;
  8. }
  9. }
Last edited by Alex Edwards : Jun 29th, 2008 at 3:11 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 46
Reputation: manzoor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
manzoor manzoor is offline Offline
Light Poster

Re: while & if statement

  #5  
Jun 29th, 2008
//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 ?
Reply With Quote  
Join Date: Jan 2008
Posts: 1,738
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 215
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: while & if statement

  #6  
Jun 29th, 2008
Originally Posted by manzoor View Post
//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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC