Solved

A newbie question....

Posted on 2000-02-18
3
193 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

867 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

21 Experts available now in Live!

Get 1:1 Help Now