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 yellowLogical Form:`a&&(...)`

Code:banana&&(fruitcolor="yellow"); // <== Error banana is undefined var banana = 1; banana&&(fruitcolor="yellow"); // <== fruitcolor is set to yellow2)

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 5Logical 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 53)

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 1Logical 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 14)

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 0Logical 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 0Alternate 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 0Logical 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 0Alternate 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 0What'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

evalutesa op b, andif true, evaluatesc

a||b

ais evaluated andif,ais falsebis evaluated.

a|b

ais evaluated andirregardless of its truth,bis evaluated

a,b

ais evaluated, and thenbis 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:

The traditional approach to conditionalsCode:var isZero=1,answer1=answer2="unknown";

isZero is compared to 0.Code:if(isZero==0){answer1="yes"}else{answer1="no"};

if the comparison is true then set answer to yes

otherwise set answer to no

The logical approach to conditionals

isZero is compared to 0.Code:isZero==0&&(answer="yes")||(answer="no");

if the comparison is true then set answer to yes

otherwise set answer to no

seems logical.