while ((a != 0) and (q<k))

R

rtillmore

Is there a way to have two different expressions in a while loop?
I.e. something like:
while ((!doneyet) and (q<k))

I tried while ((!doneyet), (q<k))
but the compiler says computed value not used and the program doesn't
work right. doneyet is a function that returns an int.

Thanks,
 
A

Andrey Tarasevich

Is there a way to have two different expressions in a while loop?
I.e. something like:
while ((!doneyet) and (q<k))

Well, isn't

while (!doneyet && q < k)
...

what you are looking for?
I tried while ((!doneyet), (q<k))
but the compiler says computed value not used and the program doesn't
work right. doneyet is a function that returns an int.

If 'doneyet' is a function, then you probably want to _call_ it, meaning
that a function call operator '()' (possibly with arguments) is needed

while (!doneyet() && q < k)
...
 
J

James Kuyper

Is there a way to have two different expressions in a while loop?
I.e. something like:
while ((!doneyet) and (q<k))

What you want is "&&", which is the traditional C way of writing "and".
However, if you want to use "and" rather than "&&", you can:

#include <iso646.h>

That header defines a number of macros that expand into commonly used
operators, including a macro named 'and' which expands into "&&".

However, since this header was added to C90 in 1995; a few compilers
might not provide it. More importantly, those macros haven't proven to
be very popular, so most C programmers aren't very familiar with them.
In the long run you'll probably cause less confusion by using "&&"
rather than "and".
I tried while ((!doneyet), (q<k))
but the compiler says computed value not used and the program doesn't
work right. doneyet is a function that returns an int.

Well, in that case the expression (!doneyet) will convert "doneyet" into
a pointer to that function. !doneyet will check whether that pointer is
null. Since it points at an actual function, it can't be null, so
!doneyet will always have a value of 0, so the condition of your while()
loop will never be met.

I suspect that you actually mean !doneyet(), which actually calls the
function, and checks the value that it returns.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top