Solved

A newbie question....

Posted on 2000-02-18
3
194 Views
Last Modified: 2010-04-15
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
Comment
Question by:drummond
3 Comments
 
LVL 2

Expert Comment

by:Ready4Dis
ID: 2535866
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
 
LVL 16

Accepted Solution

by:
imladris earned 120 total points
ID: 2535872
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
 

Author Comment

by:drummond
ID: 2536489
You're right! Thank you!

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now