Solved

A newbie question....

Posted on 2000-02-18
3
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
You're right! Thank you!

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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

10 Experts available now in Live!

Get 1:1 Help Now