# logical operators

[b][red]This message was edited by Gregry2 at 2006-6-5 6:28:6[/red][/b][hr]
A thousand years have past...lol, im back. Well, if anyone's intrested in noeing y i've been gone, they can ask...but for now, straight to the question.

In C/C++, do the logical operators (&&,||, i think...) evaluate the right side and if it is false or true, the entire expression is considered false or true, both respectively?

Theres something like that in Java, but i think its for & and |, or the other way around...

thnx
{2}rIng

oh yeah, and one more thing. I wonder which is more time wasting, a conditional, or an expression repeated twice for no reason. Pls, try to visualize it in your mind, my actual code will take much longer than this paragraph to explain...

thnx

• : [b][red]This message was edited by Gregry2 at 2006-6-5 6:28:6[/red][/b][hr]
: A thousand years have past...lol, im back. Well, if anyone's intrested in noeing y i've been gone, they can ask...but for now, straight to the question.
:
: In C/C++, do the logical operators (&&,||, i think...) evaluate the right side and if it is false or true, the entire expression is considered false or true, both respectively?
:
: Theres something like that in Java, but i think its for & and |, or the other way around...
:
: thnx
: {2}rIng
:
: oh yeah, and one more thing. I wonder which is more time wasting, a conditional, or an expression repeated twice for no reason. Pls, try to visualize it in your mind, my actual code will take much longer than this paragraph to explain...
:
: thnx
:
In C, the logical operators && and || evalutate left to right and stop as soon as the truth or falsehood of the whole expression can be determined. Some examples:
[code]
if (1 || (...))
[/code]
Here (...) won't be evaluated since the result can be determined with the left operand of the '||'.

Same thing with:
[code]
if (0 && (...))
[/code]

What concerns your second question about wasting time, I would answer it depends on the compiler. Some compilers have an option that optimizes code so that duplicate expressions are not calculated twice (see option -Oc if you use Borland compiler). Here's an extract of Borland help files:
[blue]
When this option is set, the compiler eliminates common subexpressions within groups of statements unbroken by jumps (basic blocks) and functions. This option globally eliminates duplicate expressions within the target scope and stores the calculated value of those expressions once (instead of recalculating the expression).
[/blue]

Steph
• : : [b][red]This message was edited by Gregry2 at 2006-6-5 6:28:6[/red][/b][hr]
: : A thousand years have past...lol, im back. Well, if anyone's intrested in noeing y i've been gone, they can ask...but for now, straight to the question.

[blue]Where have you been?[/blue]

: :
: : In C/C++, do the logical operators (&&,||, i think...) evaluate the right side and if it is false or true, the entire expression is considered false or true, both respectively?
: :
: In C, the logical operators && and || evalutate left to right and stop as soon as the truth or falsehood of the whole expression can be determined. Some examples:
: [code]
: if (1 || (...))
: [/code]
[blue]
I may be wrong, and it seems so, but isn't & binary and && boolean?
I think both expressions are eveluated on both.

You, or I, may have confused this with another language...
[/blue]

• : : : [b][red]This message was edited by Gregry2 at 2006-6-5 6:28:6[/red][/b][hr]
: : : A thousand years have past...lol, im back. Well, if anyone's intrested in noeing y i've been gone, they can ask...but for now, straight to the question.
:
: [blue]Where have you been?[/blue]
:
: : :
: : : In C/C++, do the logical operators (&&,||, i think...) evaluate the right side and if it is false or true, the entire expression is considered false or true, both respectively?
: : :
: : In C, the logical operators && and || evalutate left to right and stop as soon as the truth or falsehood of the whole expression can be determined. Some examples:
: : [code]
: : if (1 || (...))
: : [/code]
: [blue]
: I may be wrong, and it seems so, but isn't & binary and && boolean?
: I think both expressions are eveluated on both.
:
: You, or I, may have confused this with another language...
: [/blue]
:
You are right, & performs an AND on the bits of integers, while && is the logical AND which is used with booleans. But in C, there is no particular type for booleans, which are then integers. TRUE is the same as non-zero and FALSE is the same as zero.

Steph
• : : : : [b][red]This message was edited by Gregry2 at 2006-6-5 6:28:6[/red][/b][hr]
: : : : A thousand years have past...lol, im back. Well, if anyone's intrested in noeing y i've been gone, they can ask...but for now, straight to the question.
: :
: : [blue]Where have you been?[/blue]
: :
: : : :
: : : : In C/C++, do the logical operators (&&,||, i think...) evaluate the right side and if it is false or true, the entire expression is considered false or true, both respectively?
: : : :
: : : In C, the logical operators && and || evalutate left to right and stop as soon as the truth or falsehood of the whole expression can be determined. Some examples:
: : : [code]
: : : if (1 || (...))
: : : [/code]
: : [blue]
: : I may be wrong, and it seems so, but isn't & binary and && boolean?
: : I think both expressions are eveluated on both.
: :
: : You, or I, may have confused this with another language...
: : [/blue]
: :
: You are right, & performs an AND on the bits of integers, while && is the logical AND which is used with booleans. But in C, there is no particular type for booleans, which are then integers. TRUE is the same as non-zero and FALSE is the same as zero.
:
: Steph
:
Yes, then 2 && 1 would be 1,
and 2 & 1 would be 0.

What if I wanted to execute two funcs with
func1() && func2()
and they return 2 and 1 respectevly. Then I can't change & to &&, becouse it will be wrong.
• : : : : : [b][red]This message was edited by Gregry2 at 2006-6-5 6:28:6[/red][/b][hr]
: : : : : A thousand years have past...lol, im back. Well, if anyone's intrested in noeing y i've been gone, they can ask...but for now, straight to the question.
: : :
: : : [blue]Where have you been?[/blue]
: : :
: : : : :
: : : : : In C/C++, do the logical operators (&&,||, i think...) evaluate the right side and if it is false or true, the entire expression is considered false or true, both respectively?
: : : : :
: : : : In C, the logical operators && and || evalutate left to right and stop as soon as the truth or falsehood of the whole expression can be determined. Some examples:
: : : : [code]
: : : : if (1 || (...))
: : : : [/code]
: : : [blue]
: : : I may be wrong, and it seems so, but isn't & binary and && boolean?
: : : I think both expressions are eveluated on both.
: : :
: : : You, or I, may have confused this with another language...
: : : [/blue]
: : :
: : You are right, & performs an AND on the bits of integers, while && is the logical AND which is used with booleans. But in C, there is no particular type for booleans, which are then integers. TRUE is the same as non-zero and FALSE is the same as zero.
: :
: : Steph
: :
: Yes, then 2 && 1 would be 1,
: and 2 & 1 would be 0.
:
: What if I wanted to execute two funcs with
: func1() && func2()
: and they return 2 and 1 respectevly. Then I can't change & to &&, becouse it will be wrong.
:
I do not understand what you mean exactly in the second half of your post (sorry, I am French and my English is far from being perfect;-)).
The expression
[code]
func1() && func2()
[/code]
is computed the following way. First func1() is called. If it returns 0, the whole expression has value 0 and func2() is not called. Otherwise, func2() is called and the expression is false (value 0) if func2() returns 0, true (value 1) if it returns any other integer.
Is that what you wanted to know?

Steph
• : : Yes, then 2 && 1 would be 1,
: : and 2 & 1 would be 0.
: :
: : What if I wanted to execute two funcs with
: : func1() && func2()
: : and they return 2 and 1 respectevly. Then I can't change & to &&, becouse it will be wrong.
: :
: Your numeric examples are right.
: I do not understand what you mean exactly in the second half of your post (sorry, I am French and my English is far from being perfect;-)).
: The expression
: [code]
: func1() && func2()
: [/code]
: is computed the following way. First func1() is called. If it returns 0, the whole expression has value 0 and func2() is not called. Otherwise, func2() is called and the expression is false (value 0) if func2() returns 0, true (value 1) if it returns any other integer.
: Is that what you wanted to know?
:
: Steph
:
Yes I know that, but what if I wanted both to be called, but one of the return 2 and the other return 1.

If I do it like this:
func1() && func2()
func2 won't be called and if I do it like this:
func1() & func2()
It will return the wrong thing...
• : : : Yes, then 2 && 1 would be 1,
: : : and 2 & 1 would be 0.
: : :
: : : What if I wanted to execute two funcs with
: : : func1() && func2()
: : : and they return 2 and 1 respectevly. Then I can't change & to &&, becouse it will be wrong.
: : :
: : Your numeric examples are right.
: : I do not understand what you mean exactly in the second half of your post (sorry, I am French and my English is far from being perfect;-)).
: : The expression
: : [code]
: : func1() && func2()
: : [/code]
: : is computed the following way. First func1() is called. If it returns 0, the whole expression has value 0 and func2() is not called. Otherwise, func2() is called and the expression is false (value 0) if func2() returns 0, true (value 1) if it returns any other integer.
: : Is that what you wanted to know?
: :
: : Steph
: :
: Yes I know that, but what if I wanted both to be called, but one of the return 2 and the other return 1.
:
: If I do it like this:
: func1() && func2()
: func2 won't be called and if I do it like this:
: func1() & func2()
: It will return the wrong thing...
:
If func1() returns 2 or 1, then func2() will be called in:
[code]
func1() && func2()
[/code]
because the result of the logical AND is not known (without evaluating the second operand) if func1() returns non zero (TRUE).

What if the expression is
[code]
func1() || func2()
[/code]
with the same values returned and you want both functions to be called?
One solution could be:
[code]
res1=func1();
res2=func2();
if (res1 || res2)
...
[/code]

Steph

Yes, it wasn't really a Q....
• [b][red]This message was edited by Gregry2 at 2006-6-6 7:14:3[/red][/b][hr]
Hey hey.

I think wat niklas meant is that
(func1() && func2()) != (func1() & func2()), well, yeah,
not neccessarily.

Look, for the one about repeated expressions, well, it isnt really that, when i though about it, heres the code in question

[code]
j+*pi < *limit && j < blen
[/code]

it is the conditional expression for a for loop. its notable to say that this is an inner loop.

Okay, j is the increment var. The thing is that, as you can see, obviously there are two conditions that need to be met. But actually, under normal circumstances, the only condition needed to be met is j < blen, so ,when conditions are normal, *pi is set to point to a variable always of value 0, and limit set to blen. Funny huh? But why? See, I still need to handle the other condition. In this, pi is set to point to i, the increment of the out loop(pi, pointer to i :-) ), and their sum has to be < another variable, which limit is set to. I could have done this easier and easier looking by using an if, but that would cause the same if to be repeated either alen(the limit of the out loop)*blen, or alen*_sint_iii_(the variable pointed to by limit) times.

So thats it. Its isn't the same expression, but an equivalent one. So there.

Is my solution better? Thats what i ask, i guess.

Thanx
{2}rIng

okay, where I've been, since not all will read this, its here. I dont noe actually, I guess I've just been playing games and stuff and school and all, but school is out, so I can do programming now and finally finish that other program yes, I'm still not done. As retribution for my laziness, I havent alowed my self to program anything or even read any programming material other than references, even my new lovely Java game programming book :-( I even stopped myself from continuing playing some games, one is FFX-2 ;( lol

And, well, I guess I haven't come on because I've just been answering my questions on my own lately, through trial and error. The former question seemed self answerable, but the latter is something I need to ask you guys about. I guess I'll try to keep coming form now on.

Well thats all. Till later!