#### Archived

This topic is now archived and is closed to further replies.

# Why dosen´t this work?

This topic is 5525 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Why dosen´t this code work? if( hr == ( i || j ) ) While this does if( hr = i or hr = j ) ) It works in VB, so why not in C++?

##### Share on other sites
See my response in the other thread you created for why it doesn''t work; logical-or can return ANY nonzero value for "true".

I doubt extremely much that this works in VB; it''s much more likely that it happened to work because of the particular numbers you were using.

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
In C++ you would write that as:

if ( h == i || h == j )

##### Share on other sites
quote:
Original post by peter86
Why dosen´t this code work?
if( hr == ( i || j ) )

post your code here. what is i and j? what do you mean it doesn''t work? it returns FALSE?

quote:

While this does
if( hr = i or hr = j ) )
It works in VB, so why not in C++?

never ever use (hr = i) in an if statement if you are comparing values. Equal sign (=) in C++ is used only as an assignment operator, not comparison. Use == for comparison.
Btw..what does "or" doing there? Are you sure it''s there?

My compiler generates one error message: "does not compile."

##### Share on other sites
Well, err, what are you trying to do?

If hr, i, and j are booleans, then it would make sense. Otherwise, nuh-uh.

Your second example has the obvious error in that h = i is always going to be true because you''re using the equal operator, not the comparison operator, and unless something is REALLY wrong, equal operators will always be successful.

-fel

##### Share on other sites
quote:

While this does
if( hr = i or hr = j ) )
It works in VB, so why not in C++?

Quite frankly because C++ is not VB. Also that code will not work (AFAIK, there is no ''or'' keyword in C++ and you have an ''orphan'' bracket at the end of the if statement). Also be aware that the single ''='' denotes assignment in C++, while the double ''=='' denotes comparison (which is a relatively easy thing to shoot yourself in the foot with).

The logical or operator ''||'' in C++ determines a true or false condition of the operands being compared. If one (or both) of the operands are a non zero value, it will return true otherwise it will return false.

If you change it to:

if(hr == i||hr == j) {
//Conditional code here
}

...you should get the results you expected.

HTH,

SysOp_1101

##### Share on other sites
They are entirely two different things. Of course the results are different.

In VB:
"OR" means "|" in C
"AND" means "&" in C
(hope i''m not wrong... many years not using VB)

##### Share on other sites
I know it should be ''=='' instead of ''='', my brain were temporary offline when i wrote it .

Wouldn´t this work either?:

if( i = ( j | k ) )

##### Share on other sites
Like felisandria said: only if hr, i, and j are booleans.

bool hr, i = true, j = false;

( i || j ) is then an expression (true) which can be compared to (or assigned to) hr.

However, I will assume HR is an HRESULT, which is of course not boolean. I understand it's not how you would write it in english, but it's how it is in c++ To do what you want to do you would have to write it as igamemakr and others proposed.

[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on December 3, 2002 5:19:14 PM]

##### Share on other sites
Leaving aside your snippets of code from 2 different langs...

What your logic is saying is:

IF hr EQUALS j OR hr EQUALS i THEN (statements)

Here''s the C++ code for it.
if((hr == i) || (hr == j))
{

}

|| is the logical OR. | is the binary OR (OR''ing 2 bits together)

FYI && is logical AND, & is binary OR.

V''lion

Bugle4d

##### Share on other sites
quote:
Original post by SysOp_1101
AFAIK, there is no ''or'' keyword in C++

Actually, there is such a keyword. It''s an alternative token for ||.

##### Share on other sites
quote:
Original post by spock
Actually, there is such a keyword. It's an alternative token for ||.

Are you sure that's a standard C++ keyword? I can imagine compilers implementing this as a macro, but ...

[edited by - Miserable on December 3, 2002 6:50:28 PM]

##### Share on other sites
quote:

Actually, there is such a keyword. It''s an alternative token for ||.

I stand corrected. After consulting the ''tome of Stroustrup'' I found that the ''or'' keyword was indeed valid (as well as a few others and some ''hairy'' looking digraphs and trigraphs). Thanks for pointing that out spock. :D

##### Share on other sites
quote:
Original post by felisandria
... unless something is REALLY wrong, equal operators will always be successful.
Unless my brain is REALLY tired, (x = 0) should not evaluate to true.

##### Share on other sites
quote:
Original post by peter86
Wouldn´t this work either?:

if( i = ( j | k ) )

I still haven´t got any answear to this.

##### Share on other sites
What do you mean "it doesn''t work"? Compiler error? Wrong behaviour? Doesn''t evaluate to what you want?

Please explain what you want to achive, and the necessary context of the code you provide. We do not have a clue what types the variables are that you''re using.

First,
if( i = ( j | k ) )
This has no meaning if i, j and k are booleans, floats, etc. Otherwise, if they are integers, it will first evaluate the expression (j | k) (bitwise OR of j and k), then assign that value to i. Last, if the value of i evaluates to true (ie. not zero) it will execute the following statement or code block.

If it is that you want, it works, otherwise it doesn''t.

If you are looking for an expression that evaluates to true if i equals either of j or k, then you should have this:
if((i == j) || (i == k))

If that''s not what you''re looking for, please describe your problem properly.