Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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)
    alert('yay');

    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)

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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
    Last edited by liorean; 12-21-2005 at 09:35 AM. Reason: Had two #3 in LogicalNOTexpression
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote 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)
    alert('yay');

  • #5
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    did you try if (var1 = 5 || var2 = 5) alert('yay');

  • #6
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dazappa
    I want to be able to use multiple if statements like:

    if (var1 = 5) AND (var2 = 5)
    alert('yay');

    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)
    For your example:

    Code:
     
    if((var1 == 5) && (var2 == 5))
     {
        alert('yay')
     }
    "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

  • #7
    New Coder
    Join Date
    Dec 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    hp = GetCookie('hp');
    hpup = parseInt(hp) + 5;
    maxhp = GetCookie('maxhp');
    item1 = GetCookie('item1');
    	if ((item1 == 'Apple') && (hpup <= maxhp))
    	}
    	SetCookie('hp', hpup);
    	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?
    Last edited by dazappa; 12-16-2005 at 10:47 PM.

  • #8
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote 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?

  • #9
    New Coder
    Join Date
    Dec 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote 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;
    hp = GetCookie('hp');
    hpup = parseInt(hp) + 5;
    maxhp = GetCookie('maxhp');
    item1 = GetCookie('item1');
    	if ((item1 == 'Apple') && (hpup <= maxhp) && (blah = 3))
    	}
    	SetCookie('hp', hpup);
    	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?

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by dazappa
    so, what is the correct syntax?
    Code:
    if(a && b)
        c;
    Quote 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
    Last edited by liorean; 12-16-2005 at 11:25 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #11
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote 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;
    hp = GetCookie('hp');
    hpup = parseInt(hp) + 5;
    maxhp = GetCookie('maxhp');
    item1 = GetCookie('item1');
        if ((item1 == 'Apple') && (hpup <= maxhp) && (blah = 3))
        }
        SetCookie('hp', hpup);
        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>

  • #12
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Last edited by liorean; 12-17-2005 at 12:26 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #14
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote 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()
    {
        alert('returnsTrue() processed');
        return(true);
    }
    
    if (true || returnsTrue())
    {
        alert('short circuit OR triggered');
    }
    
    if (false && returnsTrue())
    {
        alert('short circuit AND triggered');
    }
    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 :[

  • #15
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    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
    Last edited by Basscyst; 12-17-2005 at 12:34 AM.
    Helping to build a bigger box. - Adam Matthews


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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