The usage of || or &&

Anyone got a good way to remember when to use && and when to use || in an expression? I keep on using them the wrong way round - just spent an hour debugging a script, typecasting everything… and then found this was the problem:

Spotted the mistake (Hint: the first wrong character is @|@, and the last is @|@)?

Trouble is, when I use English, I do use an OR in that situation:
“If COST_USD or COST_GBP don’t equal $amt, then fail”

So if anyone’s got a mnemonic or similar that can help me remember this, please let me know. Otherwise, I’ll just have to wait until my brain’s as twisted as that of a CS(Computer Science) student ;-)

2 comments ↓

#1 Simon Willison on 09.11.03 at 11:11 pm

It sounds like you need to do a quick course on boolean logic (Google for it). Don’t worry - it’s pretty basic as far as computer science goes. Mess around with boolean equations for a while and you’ll have no trouble remembering that or means “one or the other or both” while and means “both and only both”.

#2 Jim Dabell on 09.16.03 at 11:29 pm

Ditto to what Simon says, this is basic boolean logic. You might find the other way of expressing it easier to remember though:

$amt [ne] COST_USD or $amt [ne] COST_GBP

…is equivelant to:

not ($amt [eq] COST_USD or $amt [eq] COST_GBP)

(Your comments system is screwing up the code. Replace [ne] with exclamation mark equals sign and [eq] with double equals sign

Leave a Comment