Order of evaluting functions ?

T

tienlx

Hi all, i'm sorry about my poor English ( it isn't my native language).
I confused the order which the function in C be evaluted in for()
function ? Ex:
i have 3 functions : a() b() and c()
and i'll write something like that:
for ( ;a() && b() || c(); )

what is the order which functions is evaluted ?
if both a() and b() was true , is c() will be evaluted or not ?

is it depend on which compiler i use ?
can u recommend me which C compiler i should use, because i'm begining
to learn C and i want to expert it.


Thanks , and hope u can understand what i wrote :( !
 
M

Mike Wahler

tienlx said:
Hi all, i'm sorry about my poor English ( it isn't my native language).
I confused the order which the function in C be evaluted in for()
function ? Ex:
i have 3 functions : a() b() and c()
and i'll write something like that:
for ( ;a() && b() || c(); )

what is the order which functions is evaluted ?

First 'a()' is evaluated. If its value is true,
'b()' is evaluated. If 'b()' is also true, then
'c()' is evaluated. This has nothing to do directly
with the functions, but with the logical operators
&& and ||.
if both a() and b() was true , is c() will be evaluted or not ?
Not.


is it depend on which compiler i use ?

NO. It's a language rule.
can u recommend me which C compiler i should use, because i'm begining
to learn C

That depends on several factors, e.g. which platform you
use, your tastes, whether you're willing to spend money,
or want it for free, etc.

-Mike
 
M

Mabden

tienlx said:
Hi all, i'm sorry about my poor English ( it isn't my native language).
I confused the order which the function in C be evaluted in for()
function ? Ex:
i have 3 functions : a() b() and c()
and i'll write something like that:
for ( ;a() && b() || c(); )

what is the order which functions is evaluted ?
if both a() and b() was true , is c() will be evaluted or not ?

is it depend on which compiler i use ?
can u recommend me which C compiler i should use, because i'm begining
to learn C and i want to expert it.

Thanks , and hope u can understand what i wrote :( !

Yeah. Please don't do that. Just don't. It doesn't matter what order or
why you are doing it. It's just bad programming and I want you to stop.

If this is homework, then it is just a bad homework question; take the F
.. If it is an interview question - please don't encourage them by
working there.

Just say, "No!"
 
N

Nick Keighley

Mabden said:
Yeah. Please don't do that. Just don't. It doesn't matter what order or
why you are doing it. It's just bad programming and I want you to stop.

If this is homework, then it is just a bad homework question; take the F
. If it is an interview question - please don't encourage them by
working there.

Just say, "No!"

why?

It would be more constructive if you explained why you say this. Are we
just to assume you're a genius?
 
A

Antonio Contreras

Mike said:
First 'a()' is evaluated. If its value is true,
'b()' is evaluated. If 'b()' is also true, then
'c()' is evaluated. This has nothing to do directly
with the functions, but with the logical operators
&& and ||.


Not.

I got confused by your post. In the previous paragraph you say that if
both a() and b() are true, then c() is evaluated, and now you anser no
when asked exactly the same thing.

If I get it right, first a() is evaluated.

- If a() is true then b() is evaluated.
-If b() is true the whole expression is true. c() doesn't get
evaluated.
-if b() is false c() is evaluated and its return value (interpreted
as a
boolean value) is returned.

- If a() is false a() && b() will be false regardless of b(), so b() is
not evaluated. Then c() gets evaluated and its return value determines
the value of the whole expression.

<snip question about compiler choice>
 
M

Mac

First 'a()' is evaluated. If its value is true,
'b()' is evaluated. If 'b()' is also true, then
'c()' is evaluated.

Well, no. As you say farther down, c() is NOT executed if the value of the
overall expression is known. That is, if a() and b() both evaluate to
non-zero, then the value of the expression as a whole is known, and c()
will not be evaluated/executed.
This has nothing to do directly
with the functions, but with the logical operators
&& and ||.


Right.


NO. It's a language rule.


That depends on several factors, e.g. which platform you
use, your tastes, whether you're willing to spend money,
or want it for free, etc.

-Mike

$ cat main.c
#include<stdio.h>

int a(int i) { putchar('a'); return i; }
int b(int i) { putchar('b'); return i; }
int c(int i) { putchar('c'); return i; }

int main(void)
{
a(1) && b(1) || c(1) ? puts(" 1 && 1 || 1 -> true") : puts(" 1 && 1 || 1-> false");
a(1) && b(1) || c(0) ? puts(" 1 && 1 || 0 -> true") : puts(" 1 && 1 || 0-> false");
a(1) && b(0) || c(1) ? puts(" 1 && 0 || 1 -> true") : puts(" 1 && 0 || 1-> false");
a(1) && b(0) || c(0) ? puts(" 1 && 0 || 0 -> true") : puts(" 1 && 0 || 0-> false");
a(0) && b(1) || c(1) ? puts(" 0 && 1 || 1 -> true") : puts(" 0 && 1 || 1-> false");
a(0) && b(1) || c(0) ? puts(" 0 && 1 || 0 -> true") : puts(" 0 && 1 || 0-> false");
a(0) && b(0) || c(1) ? puts(" 0 && 0 || 1 -> true") : puts(" 0 && 0 || 1-> false");
a(0) && b(0) || c(0) ? puts(" 0 && 0 || 0 -> true") : puts(" 0 && 0 || 0-> false");
return 0;
}

$ gcc -o main main.c
$ ./main
ab 1 && 1 || 1 -> true
ab 1 && 1 || 0 -> true
abc 1 && 0 || 1 -> true
abc 1 && 0 || 0-> false
ac 0 && 1 || 1 -> true
ac 0 && 1 || 0-> false
ac 0 && 0 || 1 -> true
ac 0 && 0 || 0-> false

--mac
 
O

Omri Barel

Mabden said:
Yeah. Please don't do that. Just don't. It doesn't matter what order or
why you are doing it. It's just bad programming and I want you to stop.

If this is homework, then it is just a bad homework question; take the F
. If it is an interview question - please don't encourage them by
working there.

Just say, "No!"

While it's true that

while (a() && b() || c())

could be slightly more readable, what exactly is your alternative, in
case he really needs to test these conditions? Can you come up with a
more readable version which calls each function once, and in the right
order (that is, calling c() only if a() or b() return 0, and calling b()
only if a() returns non-zero)?

Do you think

while (1) {
int do_flag = 0;
if (a()) {
if (b()) {
do_flag = 1;
} else {
do_flag = c();
}
} else {
do_flag = c();
}
if (do_flag) {
/* body of original loop */
} else {
break;
}
}

is more readable?

It could well be a situation of

while (Stack_Pop(stack, &value) && Process(stack, value)
|| Event_Available(&event)) {
/* deal with event */
}
/* stack was empty or value was END_OF_INPUT, and no pending events */

(yes, it could also be any other situation).
 
J

Jordan Abel

Hi all, i'm sorry about my poor English ( it isn't my native language).
I confused the order which the function in C be evaluted in for()
function ? Ex:
i have 3 functions : a() b() and c()
and i'll write something like that:
for ( ;a() && b() || c(); )

what is the order which functions is evaluted ?

in the order you wrote them
if both a() and b() was true , is c() will be evaluted or not ?

no. also, if a() is false, b() will not be evaluated [but c() will].
is it depend on which compiler i use ?

Any ANSI compiler will do this, and the behavior dates back to
before C itself existed.
can u recommend me which C compiler i should use, because i'm
begining to learn C and i want to expert it.

What kind of platform are you on?
 
C

Chris Dollin

Jordan said:
in the order you wrote them

Not [necessarily] true; as you yourself note, if a() is false then
b() is not evaluated, but c() is.

Mixed with && and ||, expressions are evaluated in a left-to-right
order - but not /all/ of them need appear in that order.
 
M

Mike Wahler

Antonio Contreras said:
I got confused by your post. In the previous paragraph you say that if
both a() and b() are true, then c() is evaluated,

You've caught my mistake. Sorry and thanks.
and now you anser no
when asked exactly the same thing.

Oops. :)
If I get it right, first a() is evaluated.

- If a() is true then b() is evaluated.
-If b() is true the whole expression is true. c() doesn't get
evaluated.
Correct.

-if b() is false c() is evaluated and its return value (interpreted
as a
boolean value) is returned.

- If a() is false a() && b() will be false regardless of b(), so b() is
not evaluated. Then c() gets evaluated and its return value determines
the value of the whole expression.

I'll be more careful next time. :)

-Mike
 
D

Default User

It would be more constructive if you explained why you say this.

In fact it's vital that the new C user learn about the effects
short-circuit evaluation can have in complicated expressions.
Are we just to assume you're a genius?

That would be a very bad assumption.


Brian
 
M

Mabden

Omri Barel said:
While it's true that

while (a() && b() || c())
could be slightly more readable,

Not really. I make the same suggestion to you. Please don't.

what exactly is your alternative, in
case he really needs to test these conditions? Can you come up with a
more readable version which calls each function once, and in the right
order (that is, calling c() only if a() or b() return 0, and calling b()
only if a() returns non-zero)?

Why, yes, I could easily do so. See below.
Do you think

while (1) {
int do_flag = 0;
if (a()) {
if (b()) {
do_flag = 1;
} else {
do_flag = c();
}
} else {
do_flag = c();
}
if (do_flag) {
/* body of original loop */
} else {
break;
}
}

is more readable?

Are you serious? I think I would try to have you fired.

How about something that IS readable:
===============
do {
rc = a() && b();
if (rc)
c();
} while (rc);
===============
That took a good 5 minutes...
 
A

August Karlstrom

tienlx said:
Hi all, i'm sorry about my poor English ( it isn't my native language).
I confused the order which the function in C be evaluted in for()
function ? Ex:
i have 3 functions : a() b() and c()
and i'll write something like that:
for ( ;a() && b() || c(); )

what is the order which functions is evaluted ?
if both a() and b() was true , is c() will be evaluted or not ?

is it depend on which compiler i use ?
can u recommend me which C compiler i should use, because i'm begining
to learn C and i want to expert it.


Thanks , and hope u can understand what i wrote :( !

Your English is not a problem, but the way you use whitespace, spell
"you" as u, spell "I" as "i" and that you don't start a sentence with a
capital letter is. Compare the clarity and the impression you get when
you read the reformatted version below compared to the original version
above:

<reformatted-version>

Hi all,

I'm sorry about my poor English (it isn't my native language). I
confused the order which the function in C be evaluted in for()
function? Example: I have 3 functions a(), b() and c() and I'll write
something like

for ( ;a() && b() || c(); )

What is the order which functions is evaluted? If both a() and b() was
true, is c() will be evaluted or not? Is it depend on which compiler I
use? Can you recommend me which C compiler I should use, because I'm
begining to learn C and I want to expert it.


Thanks, and hope you can understand what I wrote :( !

</reformatted-version>


August
 
N

Netocrat

[...]
How about something that IS readable:
===============
do {
rc = a() && b();
if (rc)
c();
} while (rc);
===============
That took a good 5 minutes...

Perhaps you should have spent a little longer.
 
M

Mabden

Netocrat said:
for ( ;a() && b() || c(); )
[...]
How about something that IS readable:
===============
do {
rc = a() && b();
if (rc)
c();
} while (rc);
===============
That took a good 5 minutes...

Perhaps you should have spent a little longer.

I can say the same to you. What is wrong with that code? And why do I
even have to ask the question?
 
M

Mabden

Mabden said:
Netocrat said:
for ( ;a() && b() || c(); ) [...]
How about something that IS readable:
===============
do {
rc = a() && b();
if (rc)
c();
} while (rc);
===============
That took a good 5 minutes...

Perhaps you should have spent a little longer.

I can say the same to you. What is wrong with that code? And why do I
even have to ask the question?

Oh, C() can break out - ooops.
===============
do {
rc1 = a() && b();
if (rc1)
rc2 = c();
} while (rc1 || rc2);
===============

Better?
 
M

Mabden

Mabden said:
Mabden said:
Netocrat said:
]
for ( ;a() && b() || c(); )
[...]
How about something that IS readable:
===============
do {
rc = a() && b();
if (rc)
c();
} while (rc);
===============
That took a good 5 minutes...

Perhaps you should have spent a little longer.

I can say the same to you. What is wrong with that code? And why do I
even have to ask the question?

Oh, C() can break out - ooops.

No! it can't! If a() and b() are true, the loop goes on. What problem do
you see, damn you!
 
S

Skarmander

Mabden said:
the F



Not really. I make the same suggestion to you. Please don't.





Why, yes, I could easily do so. See below.




Are you serious? I think I would try to have you fired.

How about something that IS readable:
===============
do {
rc = a() && b();
if (rc)
c();
} while (rc);
===============
That took a good 5 minutes...

The remainder of the thread shows that this readability appears to be
overrated. I, for one, would prefer the for loop, if only because it's
easier to get right.

Your approach may be slightly clearer to people who don't understand the
short-circuit evaluation C employs, but I do not think such people need
to be accommodated.

If I really wanted to spell things out more, and assuming a() and b()
bear some relation to each other while c() is independent (which the
condition suggests), I'd do this:

while (1)
if (a() && b()) {
x();
} else if (c()) {
x();
} else {
break;
}
}

Where x() is the loop body factored out as a new function. This version
may or may not be more readable/maintainable/what have you, depending on
what exactly a(), b() and c() mean, and what x() would become.

S.
 
K

Kenny McCormack

August Karlstrom said:
Your English is not a problem, but the way you use whitespace, spell
"you" as u, spell "I" as "i" and that you don't start a sentence with a
capital letter is.

Aren't all those things components of speaking (and writing) English?
I would hazard that I learned most of those things in "English" class.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top