• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

A newbie question....

I'm new to progamming in C. I've done extensive work in Perl which is pretty similar.

My problem is that I've got a loop that doesn't seem to be working right.

while (timein !=0 && timeout !=0) {
   execute code....
}

However, it seems to be quitting too early. If "timein = 0" it will stop OR if "timeout = 0" it will stop.

I need to only stop if both conditions are met. Do I have the syntax wrong?

Extra points for my stupidty :)

Thanks!
0
drummond
Asked:
drummond
1 Solution
 
Ready4DisCommented:
try this, and lets see the code, maybe you are setting one when you don't want to..

while (! (timein==0 || timeout==0))
{
  execute code....
}
0
 
imladrisCommented:
I think your confusion lies in the while. The condition

timein!=0 && timeout!=0

will evaluate to true if both timein and timeout are nonzero. However, you want that to be your stopping condition. A while is defined as:

while(condition)
{   code
}

The while will continue to iterate executing code as long as condition evaluates to true.
As you can see your condition specifies when to stop, whereas the while expects it to specify the condition for continueing. So reversing it should suffice either:

while(timein==0 || timeout==0)

or

while(!(timein!=0 && timeout!=0))

0
 
drummondAuthor Commented:
You're right! Thank you!

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now