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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Logical conditions

    Logical conditions
    Traditional vs Logical equivalents

    Notes:
    Here are a few logic based conditonal alternatives.
    Each numbered point shows the traditional conditional form followed by the logical form.

    1)
    Traditional Form: if(a){...}

    Code:
    if(banana){fruitcolor="yellow"};  // <== Error banana is undefined
    
    var banana = 1;
    if(banana){fruitcolor="yellow"};  // <== fruitcolor is set to yellow
    Logical Form: a&&(...)
    Code:
    banana&&(fruitcolor="yellow");    // <== Error banana is undefined
    
    var banana = 1;
    banana&&(fruitcolor="yellow");    // <== fruitcolor is set to yellow
    2)
    Traditional Form: if(a>b){...}

    Code:
    var a = 101,b=5;
    if(a>100){b=25};                  // <== b is set to 25
    
    var a = 10,b=5;
    if(a>100){b=25};                  // <== b remains 5
    Logical Form: a>b&&(...)
    Code:
    var a = 101,b=5;
    a>100&&(b=25)                     // <== b is set to 25
    
    var a = 10,b=5;
    a>100&&(b=25)                     // <== b remains 5
    3)
    Traditional Form: if(a>b){...}else{...}

    Code:
    var a = 101,b=5;
    if(a>100){b=25}else{b=1};         // <== b is set to 25
    
    var a = 10,b=5;
    if(a>100){b=25}else{b=1};         // <== b is set to 1
    Logical Form: a>b&&(...)||(...)
    Code:
    var a = 101,b=5;
    a>100&&(b=25)||(b=1);             // <== b is set to 25
    
    var a = 10,b=5;
    a>100&&(b=25)||(b=1);             // <== b is set to 1
    4)
    Traditional Form: if(a>b){...; ...}else{...; ...}

    Code:
    var a = 101,b=5,c=33;
    if(a>100){b=25;c=10}else{b=1;c=0};   // <== b is set to 25 and c is set to 10
    
    var a = 10,b=5,c=33;
    if(a>100){b=25;c=10}else{b=1;c=0};   // <== b is set to 1 and c is set to 0
    Logical Form: a>b&&(...)|(...)||(...)|(...)
    Code:
    var a = 101,b=5,c=33;
    a>100&&(b=25)|(c=10)||(b=1)|(c=0);   // <== b is set to 25 and c is set to 10
    
    var a = 10,b=5,c=33;
    a>100&&(b=25)|(c=10)||(b=1)|(c=0);   // <== b is set to 1 and c is set to 0
    Alternate Logical Form: a>b&&(..., ...)||(..., ...)
    Code:
    var a = 101,b=5,c=33;
    a>100&&(b=25,c=10)||(b=1,c=0);       // <== b is set to 25 and c is set to 10
    
    var a = 10,b=5,c=33;
    a>100&&(b=25,c=10)||(b=1,c=0);       // <== b is set to 1 and c is set to 0

    5)
    Traditional Form: if(a>b){...; ...}else if(a>c){...; ...}else{...;...}

    Code:
    if(a>b){...; ...}else if(a>c){...; ...}else{...;...}
    
    var a = 101,b=5,c=33;
    if(a>100){b=25;c=10}else if(a>50){b=7;c=2}else{b=1;c=0};   // <== b is set to 25 and c is set to 10
    
    var a = 60,b=5,c=33;
    if(a>100){b=25;c=10}else if(a>50){b=7;c=2}else{b=1;c=0};   // <== b is set to 7 and c is set to 2
    
    var a = 10,b=5,c=33;
    if(a>100){b=25;c=10}else if(a>50){b=7;c=2}else{b=1;c=0};   // <== b is set to 1 and c is set to 0
    Logical Form: a>b&&(...)|(...)||a>c&&(...)|(...)||(...)|(...)
    Code:
    
    var a = 101,b=5,c=33;
    a>100&&(b=25)|(c=10)||a>50&&(b=7)|(c=2)||(b=1)|(c=0);      // <== b is set to 25 and c is set to 10
    
    var a = 60,b=5,c=33;
    a>100&&(b=25)|(c=10)||a>50&&(b=7)|(c=2)||(b=1)|(c=0);      // <== b is set to 7 and c is set to 2
    
    var a = 10,b=5,c=33;
    a>100&&(b=25)|(c=10)||a>50&&(b=7)|(c=2)||(b=1)|(c=0);      // <== b is set to 1 and c is set to 0
    Alternate Logical Form: a>b&&(..., ...)||a>c&&(..., ...)||(..., ...)
    Code:
    var a = 101,b=5,c=33;
    a>100&&(b=25,c=10)||a>50&&(b=5,c=2)||(b=1,c=0);            // <== b is set to 25 and c is set to 10
    
    var a = 60,b=5,c=33;
    a>100&&(b=25,c=10)||a>50&&(b=5,c=2)||(b=1,c=0);            // <== b is set to 7 and c is set to 2
    
    var a = 10,b=5,c=33;
    a>100&&(b=25,c=10)||a>50&&(b=5,c=2)||(b=1,c=0);            // <== b is set to 1 and c is set to 0
    What's going on with this logical condition stuff?
    A comparison is made, and if it fails the rest of the statement is ignored (alternative terms are dropped, and short-circuted).
    If the comparison succeeds, the remaining part of the statment is processed.

    All logical statments are evaluated to true or false.

    The meaning of the symbols "&&", "||", "|", and ",".

    a op b&&c
    evalutes a op b, and if true, evaluates c

    a||b
    a is evaluated and if a is false, b is evaluated.

    a|b
    a is evaluated and irregardless of its truth, b is evaluated

    a,b
    a is evaluated, and then b is evaluated


    below, c and d are statements

    a op b&&(c)
    evalutes a op b, and if true, evaluates c

    a op b&&(c)||(d)
    evalutes a op b, and if true, evaluates c - otherwise d is evaluated.

    a op b&&(c)|(d)
    evalutes a op b, and if true, evaluates c, and then d.

    a op b&&(c,d)
    evalutes a op b, and if true, evaluates c, and then d.



    Here's a final example:
    Code:
    var isZero=1,answer1=answer2="unknown";
    The traditional approach to conditionals
    Code:
    if(isZero==0){answer1="yes"}else{answer1="no"};
    isZero is compared to 0.
    if the comparison is true then set answer to yes
    otherwise set answer to no


    The logical approach to conditionals
    Code:
    isZero==0&&(answer="yes")||(answer="no");
    isZero is compared to 0.
    if the comparison is true then set answer to yes
    otherwise set answer to no


    seems logical.
    Last edited by rdspoons; 09-08-2012 at 06:14 AM.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,677
    Thanks
    6
    Thanked 1,008 Times in 981 Posts
    Interesting. But I don’t see the point except making stuff less readable.

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts
    It's good to know the different forms the language can take.
    But, do use the traditional form, especially if you need speed.
    Simple timing tests show the logical form execution speed can be over 2 times slower than the traditional (if) form.
    The traditional if structure has been optimized for performance over the years.


  •  

    Tags for this Thread

    Posting Permissions

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