while loop issue

Thread Solved
Reply

Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

while loop issue

 
0
  #1
Jan 11th, 2009
Can I do this?

  1. while(variable=functionCall()!=0){
  2.  
  3. /* ... */
  4.  
  5. }

the function I'm calling, is returning a 3, but the variable in the while loop is equal to 1...
I'm not sure what is happening in this statement, Originally I thought it would just always evaluate to true and loop forever since, variable would be successfully set to functionCall()'s return value, therefore evaluating to 1, and 1 does not equal zero, so that too would evaluate to 1 (true) and loop forever... but it sets variable to 1 and then exits the while loop--what's going on?

functionCall() gets input from the user so I can only call it once really, I was going for while(functionCall()!=0){ } , but then I wouldn't be able to check the return value within the loop. I could do something else but I wanted to use this method, so I'm just wondering what it's actually doing.
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

Re: while loop issue

 
0
  #2
Jan 11th, 2009
variable would be successfully set to functionCall()'s return value
no way, variable is set to true, because functionCall()!=0 is true. in this case true means 1
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

Re: while loop issue

 
0
  #3
Jan 11th, 2009
p.s. And yes, this does loop forever if functionCall() really returns 3
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: while loop issue

 
0
  #4
Jan 11th, 2009
you should try something like this:
  1. while ((variable = functionCall())!=0)
...
because it seems to me that variable is given the value of evaluation: functionCall()!=0, so you just need to use one more pair of brackets...
I hope this helps
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: while loop issue

 
0
  #5
Jan 11th, 2009
@da penguin, oh right, I forgot about right->left order, and you're right it was looping forever--there are two loops working together and I was mistaken with which loop I was in at what time.

@Alibeg, it worked great, thank you!
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC