PDA

View Full Version : In a String why "+" and not "&&"



.soulty
September 10th, 2003, 03:03 AM
As i was playing around , "trying" to help out dave in the other thread "Email form, button with If statement" I came across a conclusion ( with the help from Digitalosophy (see code below))

but what confused me is that i used "+" in a string to check all 3 conditions . Originally i thought that "&&" would of been used instead of "+" to check all 3 conditions but it didnt seem to work. Is there a reason why ? or when one or the other should be used.



on (press) {
if (!sender_title.length + !sender_artist.length + !party_location.length) {
stop();
} else {
play();
}
}


What i have read up was that "+" is called a addition or string concatenation (or to link together)

Can anyone clear this up for me.

thanks.

::Soulty:::smirk::

grandsp5
September 10th, 2003, 03:24 AM
True and false are assigned numerical values, false being zero, true being one. So in this case, it adds the sum of the values and only if the sum is greater than zero will it execute the statement. if statements will evaluate only if the condition is not equal to zero. That is why this works.

.soulty
September 10th, 2003, 03:50 AM
Ok , then why wouldnt "&&" operator (instead of "+" )work ?

Would that be only if you were testing two conditions? if so how would you set it up to check more than 2 conditions?

grandsp5
September 10th, 2003, 03:55 AM
The && operator would work as well. You could also write it as

if(condition1 && condition2 && condition3)


&& statements are pretty straighforward, it will execute the first statements, make that a boolean, and execute the next. So it will basically be:
condition1 && condition2

(results of condition1 && condition2) && condition 3

It is with or statements that things get a little confusing. I think Sen wrote something on this a while back, might look for it under the best of senocular.

Should work using &&. This script says, if any one of these values equals zero, then stop. Otherwise, play.

Eric Jr.
September 10th, 2003, 03:57 AM
Originally posted by Soulty
Ok , then why wouldnt "&&" operator (instead of "+" )work ?

Would that be only if you were testing two conditions? if so how would you set it up to check more than 2 conditions?

The us of brackets "()" allows you to test multiple conditions. Bare in mind that both conditions have to return a single value for the if() to proceed:

if ((condition1) && (condition2)) // both conditions returned true;
if ((condition1) || (condition2)) // Either one returned true;

grandsp5
September 10th, 2003, 04:03 AM
!sender_title.length + !sender_artist.length + !party_location.length is the same as
sender_title.length && sender_artist.length && party_location.length I believe

.soulty
September 10th, 2003, 04:04 AM
say as the above example , which has 3 conditions, in which way can you test to see that all 3 are false.

I understand what both grandsp5 and Eric Jr. has said , but

based on that this code should work



on (press) {
if ((!sender_title.length && !sender_artist.length) && (!party_location.length)) {
stop();
} else {
play();
}
}


which dosent seem to work. :-\

grandsp5
September 10th, 2003, 04:07 AM
take out the !'s and I think it will work.

.soulty
September 10th, 2003, 04:11 AM
yep that worked , switch around the statment as well :)



on (press) {
if ((sender_title.length && sender_artist.length) && (party_location.length)) {
play();
} else {
stop();
}
}

Eric Jr.
September 10th, 2003, 04:12 AM
Lol, so you need to check for length? I thought you wanted to know if they were empty

grandsp5
September 10th, 2003, 04:13 AM
parenthesis shouldnt be needed but if you have em just leave em

.soulty
September 10th, 2003, 04:16 AM
thanks guys for clearing that up :)


Originally posted by grandsp5
it adds the sum of the values and only if the sum is greater than zero will it execute the statement. if statements will evaluate only if the condition is not equal to zero. That is why this works.

though still a little bit confused on the "+" , based on the first code and what grandsp5 said how can you add a condition when its a field that is for user input?

grandsp5
September 10th, 2003, 04:29 AM
you are adding values. An if statement will only execute if the condition is not equal to zero (true) If the condition is zero(false) it wont execute. Something I forgot to mention is that the opposite of any nonzero number is zero and the opposite of zero is 1. (by opposite i mean logical negation) so !2 = 0 and !0 = 1 and !-1 = 0 and !5 = 0 etc etc. The first code negates all the values then adds them up. The else statement will only execute if all those statements equal zero aka all of them have a length greater than zero aka !field.length=0. Those 3 statements are the same.

grandsp5
September 10th, 2003, 04:33 AM
play around with tracing some values. That should help clear it up if you still dont get it. Im turning in seeing as its about 2am so night all.

.soulty
September 10th, 2003, 04:46 AM
ah ok, starting to understand it, thanks grandsp5 , will play around with it , trace some values like you said, thanks again for the help.. :thumb: nite dude. :)

Thanks to Eric Jr. for your help as well. :)

.soulty
September 10th, 2003, 04:53 AM
Originally posted by Eric Jr.
Lol, so you need to check for length? I thought you wanted to know if they were empty

Just noticed your post, well yeah wanted to check if the fields were empty, and if all 3 fields where empty to stop, until all 3 fields have been inputted by the user then it would play, the first code posted was from Digitalosophy.

Do you have something else in mind?