Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: how to use multiple if statements, no not else if

1. how to use multiple if statements, no not else if

I want to be able to use multiple if statements like:

if (var1 = 5) AND (var2 = 5)

so I want to be able to check 2 or more things, and if both are true continue on and perform some code. (and this is different topic than last thread so please dont get mad)

• By using the boolean logic operators

LogicalANDexpression: expression1 && expression2

Which does the following:
1. Evaluate expression1
2. Convert the result of #1 to Boolean
3. If the result of #2 is false, return the result of #1
4. Evaluate expression2
5. Return the result of #4

LogicalORexpression: expression1 || expression2

Which does the following:
1. Evaluate expression1
2. Convert the result of #1 to Boolean
3. If the result of #2 is true, return the result of #1
4. Evaluate expression2
5. Return the result of #4

LogicalNOTexpression: ! expression

Which does the following:
1. Evaluate expression
2. Convert the result of #1 to Boolean
3. If the result of #2 is true, return false
4. Return true

• Liorean,

I don't agree with your definition of the boolean operators.

A "logical and" (&&) will only return true if both expression are true. If either expression 1 or expression 2 is false the result will be false.

A "logical or" (||) will return true if either or both expressions is true.

• Originally Posted by PhotoJoe47
Liorean,

I don't agree with your definition of the boolean operators.

A "logical and" (&&) will only return true if both expression are true. If either expression 1 or expression 2 is false the result will be false.

A "logical or" (||) will return true if either or both expressions is true.
so, what is the correct syntax? is it:

if (var1 = 5) || (var2 = 5)

• did you try if (var1 = 5 || var2 = 5) alert('yay');

• Originally Posted by dazappa
I want to be able to use multiple if statements like:

if (var1 = 5) AND (var2 = 5)

so I want to be able to check 2 or more things, and if both are true continue on and perform some code. (and this is different topic than last thread so please dont get mad)

Code:
```
if((var1 == 5) && (var2 == 5))
{
}```
"var1 = 5" will set the value of var1 to 5. To test if var1 is equal to the value of 5 you have to use "==" the equality conditional test symbol

• Joe yes I did know i needed == ok well anyways, I tried to implement your code into part of my script and I got syntax error. Just to let you know, I have some functions in there, SetCookie and GetCookie that i know work, and I will not post them in the following code. I am posting the troublesome part.
Code:
```function use1() {
var hpup
hpup = parseInt(hp) + 5;
if ((item1 == 'Apple') && (hpup <= maxhp))
}
alert('You ate the apple and restored 5hp');
{
else
alert('You do not have a usable item');

}```
Edit: WAIT look at my if statement lol i got the braces backwards >.> ok that fixed that problem >.> but look at the second part of my if statement anyways. I use <= but is there anyway that lets say my hp was 19. How would I make it so it wouldnt go to 24?

• Originally Posted by dazappa
Joe yes I did know i needed == ok well anyways, I tried to implement your code into part of my script and I got syntax error. ......
What did the syntax error point to? Can you cut the error message from the javascript console and paste it into a message reply here?

• Originally Posted by PhotoJoe47
What did the syntax error point to? Can you cut the error message from the javascript console and paste it into a message reply here?
The syntax error only stated a line number. I went that line number and discovered that my braces were backwards. Look carefully at my if statement and you will notice i switched them. After fixing that stupid typo the program worked. but i have a question about (&&) Could I have for example...
Code:
```function use1() {
var hpup
blah = 3;
hpup = parseInt(hp) + 5;
if ((item1 == 'Apple') && (hpup <= maxhp) && (blah = 3))
}
alert('You ate the apple and restored 5hp');
{
else
alert('You do not have a usable item');

}```
is it possible to have 3 comparisons like the above example?

• Originally Posted by dazappa
so, what is the correct syntax?
Code:
```if(a && b)
c;```
Originally Posted by PhotoJoe47
Liorean,

I don't agree with your definition of the boolean operators.
Then see me prove by exampe that I am, in fact, right. Let's just run the following code:
Code:
```var
o={
'false':false,
'true':true,
'null':null,
'{}':{}},
a=[],
i,
j;

for(i in o){
for(j in o){
a.push(i+' && '+j+': '+(o[i]&&o[j]));
a.push(i+' || '+j+': '+(o[i]||o[j]));
}
a.push('! '+i+': '+(!o[i]))
}

a.join('\n');```
The results will be a list of all possible boolean logic operations with the four values: false (boolean, casts to false), true (boolean, casts to true), null (object, casts to false), object literal (object, casts to true).

The list, if we look only on the && operator, will look like this:
Code:
```false && false: false
false && true: false
false && null: false
false && {}: false
true && false: false
true && true: true
true && null: null
true && {}: [object Object]
null && false: null
null && true: null
null && null: null
null && {}: null
{} && false: false
{} && true: true
{} && null: null
{} && {}: [object Object]```
What can be read out of this? Well, first of all, the results for false as first operand all return false. To go through the list:
1. It evaluates the first expression (false).
2. false cast to boolean is false
3. #2 is false, so it returns #1 (false)

The results for null as first operand behave similarly, they all return null. To go through the list:
1. It evaluates the first expression (null)
2. null cast to boolean is false
3. #2 is false, so it returns #1 (null)

The results for true as well as the object literal as first operand we see return whatever is the value of the second operand in all cases. To go through the list:
1. It evaluates the first expression (true or obect)
2. (true or object) cast to boolean is true
3. #2 is true
4. It evaluates the second expression
5. It returns #4

The list, if we look only on the || operator, will look like this:
Code:
```false || false: false
false || true: true
false || null: null
false || {}: [object Object]
true || false: true
true || true: true
true || null: true
true || {}: true
null || false: false
null || true: true
null || null: null
null || {}: [object Object]
{} || false: [object Object]
{} || true: [object Object]
{} || null: [object Object]
{} || {}: [object Object]```
What can be read out of this? Well, first of all, the results for true as first operand all return true. To go through the list:
1. It evaluates the first expression (true).
2. true cast to boolean is true
3. #2 is true, so it returns #1 (true)

The results for object literal as first operand behave similarly, they all return the object. To go through the list:
1. It evaluates the first expression (object)
2. object cast to boolean is true
3. #2 is true, so it returns #1 (object)

The results for false as well as null as first operand we see return whatever is the value of the second operand in all cases. To go through the list:
1. It evaluates the first expression (false or null)
2. (false or null) cast to boolean is false
3. #2 is false
4. It evaluates the second expression
5. It returns #4

• Originally Posted by dazappa
The syntax error only stated a line number. I went that line number and discovered that my braces were backwards. Look carefully at my if statement and you will notice i switched them. After fixing that stupid typo the program worked. but i have a question about (&&) Could I have for example...
Code:
```function use1() {
var hpup
blah = 3;
hpup = parseInt(hp) + 5;
if ((item1 == 'Apple') && (hpup <= maxhp) && (blah = 3))
}
alert('You ate the apple and restored 5hp');
{
else
alert('You do not have a usable item');

}```
is it possible to have 3 comparisons like the above example?
Yes you can, you can have as many as you want.

Try this test code:
Code:
```
<script type="text/javascript">
var x = 0;
var y = 10;
for( i= 1; i < 7; i++)
{
x = x + 1;
y = y - 1;
if((x==5) && (y==5) && (i==5))
{
alert('x , y and i are all equal and they all equal 5')
}
else
{
alert('x = ' + x + ' y = ' + y + ' i = ' + i)
}
}
</script>```

• Liorean,

I can't seem to follow your logic. I sounds like double talk to me, but that could be because by brain is getting old.

But I stand by my defination of "logical and" and "logical or"

From what I understand javascript will check the first expression first.

With the && if the first expression is false, it stops right there and returns false. If it is true then it checks the next expression, if that expression is false it returns false, if it is true and it is the last expression it returns true.

With the || if the first expression is ture, it stops right there and returns true. If is false then it checks the next expression, if that expression is true it returns ture, if it is false and it is the last expression it will return false.

• Well, null && {} returns null, and the second operand will not be evaluated.

For null || {}, the second operand will be evaluted and be used as the return value.

The boolean logic operators treat their operands as if they were booleans, but they return the actual operands and not their boolean representation. They only return true or false if those were the actual operands sent to them.

Thus, to define the behavior of &&:
If first operand converted to boolean is false it returns the first operand (and not false).
Otherwise it returns the second operand.

And to define the behavior of ||:
If first operand converted to boolean is true it returns the first operand (not true).
Otherwise it returns the second operand.

Which is just what I wrote as those five steps above, but in a more plain English variant.

• Originally Posted by liorean
By using the boolean logic operators

LogicalANDexpression: expression1 && expression2

Which does the following:
[...]
3. If the result of #2 is false, return the result of #1
[...]

LogicalORexpression: expression1 || expression2

Which does the following:
[...]
3. If the result of #2 is true, return the result of #1
[...]
This is true for logical AND and OR, but backwards for logical short circuit AND and OR like && and ||, where the order of parsing is left to right. The difference becomes apparent when functions are evaluated as booleans:

Code:
```function returnsTrue()
{
return(true);
}

if (true || returnsTrue())
{
}

if (false && returnsTrue())
{
}```
Running this code, the only alert is 'short circuit OR triggered', proving argument 2 is never evaluated.

&& : if the result of #1 is true return the result of #2, else return false
||: if the result of #1 is false return the result of #2, else return true

edit: beaten :[

• I'm gonna have to side with Joe here, while your explanation is quite lengthy, I really don't see how it proves at any point where, coming from your example of proper syntax:

Code:
```if(a && b)
{
c;
}```
That there is ever a time where both a and b don't return true but c is still run. Unless we both are misinterpreting what you are saying. I mean in all the JS tutorials I've read, never have I seen a 1/2 page post on the utilization of &&.

Basscyst

•
Page 1 of 2 12 Last

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•