...

View Full Version : Logical conditions



rdspoons
09-08-2012, 12:33 AM
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){...}


if(banana){fruitcolor="yellow"}; // <== Error banana is undefined

var banana = 1;
if(banana){fruitcolor="yellow"}; // <== fruitcolor is set to yellow

Logical Form: a&&(...)


banana&&(fruitcolor="yellow"); // <== Error banana is undefined

var banana = 1;
banana&&(fruitcolor="yellow"); // <== fruitcolor is set to yellow


2)
Traditional Form: if(a>b){...}


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&&(...)


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{...}


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&&(...)||(...)


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{...; ...}


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&&(...)|(...)||(...)|(...)


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&&(..., ...)||(..., ...)


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{...;...}


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&&(...)|(...)||(...)|(...)




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&&(..., ...)||(..., ...)


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:


var isZero=1,answer1=answer2="unknown";


The traditional approach to conditionals


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


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.

VIPStephan
09-09-2012, 12:09 PM
Interesting. But I don’t see the point except making stuff less readable. :D

rdspoons
09-09-2012, 02:53 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum