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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need simple example, i = 2, or = 6, etc.... 3

Status
Not open for further replies.

cramd2

Programmer
Jun 10, 2005
51
US
Group,
I'm looking for a simple example to convert this logic to a javascript:
if
i != 3 or != 4
then
process more code

OR

if i = 2, or = 5, or = 6
then
process more code

I have found examples of (if (i ==2)) then process, but I have multiples that I need, such as (if i==2, or ==5, or ==6), but I cannot find correct syntax on a statement like this.
Thanks
cramd2
 
The simple way is to use two if statements.
The double pipe || is an OR statement.

Code:
if (i != 3 || i != 4) {
  do something;
}

if (i == 2 || i ==5 || i == 6) {
  do something;
}

Stamp out, eliminate and abolish redundancy!
 
Code:
if(i != 3 [!]||[/!] i!= 4)

Code:
if(i==2 [!]||[/!] i==5 [!]||[/!] i==6)

Those are vertical bars representing the phrase "or"

'hope that helps.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Maxwell Smart said:
Missed it by this much.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
I almost didn't even TRY to answer thread216-1213621. I figured you were on it even as I was quoting Maxwell Smart!

[rofl]

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
I was just leaving work. Things slow down in the afternoon so I just check when I get home for any problems someone posted late that I can answer so they do not wait until the next day for an answer. :)


Stamp out, eliminate and abolish redundancy!
 
Wait a second, did you mean [tt]i != 3[/tt] or [tt]i != 4[/tt]([tt]i[/tt] would have to equal BOTH 3 AND 4 {not possible} for this to be false)?
Or did you mean [tt]i != 3 or 4[/tt](this would be false if [tt]i[/tt] equals either 3 OR 4)?

This makes a big difference, as:
Code:
[tt]if(i != 3 || i != 4){[COLOR=red]do stuff[/color];};[/tt]
Will always execute [tt]do stuff[/tt] as it is impossible for [tt]i[/tt] to equal both numbers;
but:
Code:
[tt]if(i != 3 && i != 4){[COLOR=red]do stuff[/color]};[/tt]
Will only execute [tt]do stuff[/tt] if [tt]i[/tt] is neither 3 or 4.




I hope this helps;
Rob Hercules
 
...thus, for [tt]i[/tt] = 4 or 5 or 6, you would use
Code:
[tt]if(i == 4 [COLOR=green][b]||[/b][/color] i == 5 [COLOR=green][b]||[/b][/color] i == 6){[COLOR=red]do stuff[/color];};[/tt]
but for [tt]i[/tt] != 2 or 3 or 4, you would use
Code:
[tt]if(i != 2 [COLOR=green][b]&&[/b][/color] i != 3 [COLOR=green][b]&&[/b][/color] i != 4){[COLOR=red]do stuff[/color];};


Does this make sense for you?




I hope this helps;
Rob Hercules
 
Good catch robherc, I was more focused on the how-to than the actual output.


Stamp out, eliminate and abolish redundancy!
 
I just got back. What the heck happened here!!!

As originally posted, the question was !=3 or !=4. Of course, as Rob astutely noticed, this is sort of nonsense, since when i is 4, it is NOT 3 and passes the if-condition. Likewise, when i is 3, it is NOT 4.

I'll take the star, cramd2, but if I deserve one, certainly there are two others that deserve it more! thenightowl for answering the question you asked first and robherc for answering the question you MEANT!

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Group,
All the posts were helpful, although the logic in my example was not good, I just needed a simple example using the "and" & "or" in a statement. I have found that the javascript is not the same as other languages that I've used with the "and" & "or".
Thanks for all the useful examples!
cramd2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top