Jump to content
  • 0

&&(and) ||(or) doubt


Question

Posted

hI.

I have a doubt:

if I do && in a script like:

if (@nx =! @n1 && @n2 && @n3 && @n4 && ect....) {

is it going to consider true when @nx =! [@n1 and @n2 and @n3, and so on...] like it have to be always the same value for each different variable... right?

I need it to see if @nx is different from every single variable!

like saying that there MUST not be a variable with the same value of @nx in the list.

I thought that "||" could work but I'm afraid that it will consider only @n1 "OR" @n2 "OR" @n3

I mean: for example: @nx =! @n4 but it is = to @n3 will be the function activated anyway?

Tiny problem but so hard to describe :D

Thanks

Jeffo

9 answers to this question

Recommended Posts

Posted

You must actually do a check for each variable.

You can use AND logic to make sure the value doesn't exist or OR logic to check if any contains the value.

if(@nx != @n1 && @nx != @n2 && ....)
if(@nx == @n1 || @nx == @n2 || ...)

Simple variable checks such as 'var && var && var' checks if they are not 0.

With the first code:

if (@nx != @n1 && @n2 && @n3 && @n4 && ect....) {

You check if @nx != @n1 and then you check that @n2 != 0, @n3 != 0 and so on.

  • Upvote 1
Posted

Yes, && (AND) is the correct operator if you want to check "if @nx is not equal to @n1, not equal to @n2, not equal to @n3".

if (@nx!=@n1 && @nx!=@n2 && @nx!=@n3) {

If you take the "not" part out and group the rest in parenthesis then you would use || (OR):

if ( !(@nx==@n1 || @nx==@n2 || @nx==@n3) ) {

"if not (@nx equal to @n1, or @nx equal to @n2, or @nx equal to @n3)"

Posted

@ BrianL

i have a question

is it possible to improve that or will rA improve that

as you said

if ( !(@nx==@n1 || @nx==@n2 || @nx==@n3) ) {

like making it short

since @nx is repeated 3x

is it possible to make

if( !(@nx == @n1,@n2,@n3) ) {

Posted

No I don't think we'll be changing the syntax.

And if anything was changed, it would NOT be

if( !(@nx == @n1,@n2,@n3) ) {

Most languages that do support checking multiple values have a separate operator or function.

ex: SQL - IF @nx NOT IN (@n1,@n2,@n3)

Posted

@KeiKun

It's bad to implement some things like this (the script engine is already enough weird).

BUT you can make functions for stuff like this.

function equal {
set .@i,1;
while ( getarg(.@i,0) == getarg(.@i,1) ) {
	if ( getarg(.@i) != getarg(0) )
		return 0;
	set .@i, .@i+1;
}
return 1;
}

if ( equal(.@a,.@b,.@c,.@d,.@e) ) {


}

  • Upvote 2
Posted

You can also do some tricks if you're really lazy.

Something like this:

if(a == b && a == c && a == d && a == e && a == f)

can be written as this:

if(5*a - b - c - d - e - f == 0)

However if quickly gets obfuscated, so I wouldn't recommend it.

If the Athena script offers bitwise operators it's also possible to check multiple settings with bitmasks. :D

Posted (edited)

[move a part to another topic]

@outofcuriosity and Brian

It's not always true:

if ( (5*5 - 3 - 7 - 5 - 6 - 4) == 0) // true but no all values = 5.

Edited by KeyWorld
  • Upvote 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...