Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do Combinations of ¦¦ and && Work?

Status
Not open for further replies.

Luckygold6

Programmer
Jan 9, 2003
31
US
What order do they work in. For example, if I entered the following code, how will it execute?

if (arArray[iC][1] == 0 || arArray[1][iB] == 0 && iC < 5 && iB < 5)

When it reads the code, does it read it as:
1. arArray[iC][1] == 0 or arArray[1][iB] == 0 must hold true and iC < 5 and iC < 5 both must hold true.
or as:
2. arArray[iC][1] == 0 or arArray[1][iB] == 0 and iC < 5 and iB < 5 must hold true.

I can't seem to figure out which logic path the computer would take. Can someone please help clarify this.

Thanks in advance.
 
The computer takes the same logic as we learned at school.
If the operators are of same importance (like || and &&) it reads from left to right.
Greetings,
Rick
 
The operator precedence is
1.Boolean operators &&, ||, !
2.Arithmetic operators +, -, *, /, %
3.Equality operators <, >, ==, <=, >=, !=
4.Assignment operators =, +=, -=, *=, /=, %=

1.Highest precedence
4.Lowest precedence

 
Areza is correct but it is a bit difficult to follow

&& has a higher precedence then || so
if arArray[iC][1] == 0 || arArray[1][iB] == 0 && iC < 5 && iB < 5)

arArray[1][iB] == 0 && iC < 5 && iB < 5 is evaluated first and if either that is true OR arArray[ic][1] == 0 you will get into the if. Remember parens will be your best friend if you want to control how this is done.


Matt

 
Absolutely! Extra parentheses don't hurt your code performance one jot, but they really really aid unambiguity and readability!
 
Wow... thank guys.

I never knew parentheses could be used in those areas of th code!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top