Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #37980 > unrolled thread

Logical conundrums if () else if()

Started byJonas Thörnvall <jonas.thornvall@gmail.com>
First post2019-12-28 00:40 -0800
Last post2020-01-19 01:22 +0100
Articles 13 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  Logical conundrums if () else if() Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-12-28 00:40 -0800
    Re: Logical conundrums if () else if() "R.Wieser" <address@not.available> - 2019-12-28 12:21 +0100
      Re: Logical conundrums if () else if() Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-12-28 05:32 -0800
        Re: Logical conundrums if () else if() "R.Wieser" <address@not.available> - 2019-12-28 19:10 +0100
          Re: Logical conundrums if () else if() Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-12-28 12:16 -0800
            Re: Logical conundrums if () else if() "R.Wieser" <address@not.available> - 2019-12-29 08:52 +0100
            Re: Logical conundrums if () else if() Arno Welzel <usenet@arnowelzel.de> - 2019-12-30 21:35 +0100
              Re: Logical conundrums if () else if() Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-12-30 12:42 -0800
    Re: Logical conundrums if () else if() "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2019-12-28 09:37 -0800
      Re: Logical conundrums if () else if() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-12-29 11:43 +0100
        Re: Logical conundrums if () else if() "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2019-12-29 08:14 -0800
    Re: Logical conundrums if () else if() Soviet_Mario <SovietMario@CCCP.MIR> - 2020-01-18 21:06 +0100
      Re: Logical conundrums if () else if() Soviet_Mario <SovietMario@CCCP.MIR> - 2020-01-19 01:22 +0100

#37980 — Logical conundrums if () else if()

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-12-28 00:40 -0800
SubjectLogical conundrums if () else if()
Message-ID<cd84bfc2-63ac-4a4f-bd76-9b144035b9ba@googlegroups.com>
How can two if statements that work perfectly standalone as single statments.
Make outcome that differs when you put in, an else if statement to only execute one of them. The below statements have the statement with heavier conditioning first so not the second statement gets executed. Because both satisfy the noneboolean condition.

I probably done some very no no thingy.

 	

if (EDITEND<SELEND && document.getElementById("lockedit").checked){
  SELLENGTH=SELEND-SELSTART;
  document.getElementById("SB").value=EDITSTART+SELLENGTH;
  document.getElementById("EB").value=SELEND;
} else if (EDITEND<SELEND){ document.getElementById("EB").value=SELEND;}

[toc] | [next] | [standalone]


#37981

From"R.Wieser" <address@not.available>
Date2019-12-28 12:21 +0100
Message-ID<qu7drc$frb$1@gioia.aioe.org>
In reply to#37980
Jonas,

> How can two if statements that work perfectly standalone as
> single statments. Make outcome that differs when you put in,
> an else if statement to only execute one of them.

Exactly like you've already shown (on other words: whats the problem ?)

> The below statements have the statement with heavier conditioning
> first so not the second statement gets executed.

Thats how you should do it, yes.

> Because both satisfy the noneboolean condition.

That does not make sense: /ALL/ conditions are boolean.

> I probably done some very no no thingy.

I still have no idea what your problem is (other than a very vage notion 
that /probably/ something doesn't quite work like it should - but I'm not 
even sure of that ...)

Suggestion: SIMPLIFY THE PROBLEM.

After you put the problem into its own HTML file replace the "do this" and 
"do that" with something you can easily check, like putting up message boxes 
with a different text for the above.    Replace the condition with simpler 
versions.  Like

(EDITEND<SELEND && document.getElementById("lockedit").checked)

with (something like)

( 5 < 7 && true)

Now you can easily see *and alter* what the condition is.

You might want to see if  "( (5 < 7) && true)" works better (read: check if 
"<" has a higher priority than "&&")

Writing programs is easy. The really hard part is to hunt bugs. :-)

Regards,
Rudy Wieser

P.s.
If-and-when you have a problem start with printing the "values" you are 
comparing as well as the comparision itself.  Like a message box just above 
that code example of yours displaying this:

alert( EDITEND +" , "+ SELEND +" , "+ 
document.getElementById("lockedit").checked +" = "+ (EDITEND<SELEND && 
document.getElementById("lockedit").checked) );

[toc] | [prev] | [next] | [standalone]


#37982

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-12-28 05:32 -0800
Message-ID<0d26314f-6691-4428-90c9-b6a83828f776@googlegroups.com>
In reply to#37981
Den lördag 28 december 2019 kl. 12:21:22 UTC+1 skrev R.Wieser:
> Jonas,
> 
> > How can two if statements that work perfectly standalone as
> > single statments. Make outcome that differs when you put in,
> > an else if statement to only execute one of them.
> 
> Exactly like you've already shown (on other words: whats the problem ?)
> 
> > The below statements have the statement with heavier conditioning
> > first so not the second statement gets executed.
> 
> Thats how you should do it, yes.
> 
> > Because both satisfy the noneboolean condition.
> 
> That does not make sense: /ALL/ conditions are boolean.
> 
> > I probably done some very no no thingy.
> 
> I still have no idea what your problem is (other than a very vage notion 
> that /probably/ something doesn't quite work like it should - but I'm not 
> even sure of that ...)
> 
> Suggestion: SIMPLIFY THE PROBLEM.
> 
> After you put the problem into its own HTML file replace the "do this" and 
> "do that" with something you can easily check, like putting up message boxes 
> with a different text for the above.    Replace the condition with simpler 
> versions.  Like
> 
> (EDITEND<SELEND && document.getElementById("lockedit").checked)
> 
> with (something like)
> 
> ( 5 < 7 && true)
> 
> Now you can easily see *and alter* what the condition is.
> 
> You might want to see if  "( (5 < 7) && true)" works better (read: check if 
> "<" has a higher priority than "&&")
> 
> Writing programs is easy. The really hard part is to hunt bugs. :-)
> 
> Regards,
> Rudy Wieser
> 
> P.s.
> If-and-when you have a problem start with printing the "values" you are 
> comparing as well as the comparision itself.  Like a message box just above 
> that code example of yours displaying this:
> 
> alert( EDITEND +" , "+ SELEND +" , "+ 
> document.getElementById("lockedit").checked +" = "+ (EDITEND<SELEND && 
> document.getElementById("lockedit").checked) );

Hello Rudy, I can explain in short what it do, it is an incrementor for a bar in two modes in one mode it keep the barlength, moving backend with front end. In other mode the bar is expanded forward.

Both modes work well with "single if statements", but when used together in the else if, it all break down.

You can try here step forward with Sel page + and Sel page -
But the original code(use that expanded the scroll in the else clause for some reason bug out "although work perfectly well standalone". Uncheck Lock Editbar and the bug appear.

https://midisequenser.000webhostapp.com

[toc] | [prev] | [next] | [standalone]


#37984

From"R.Wieser" <address@not.available>
Date2019-12-28 19:10 +0100
Message-ID<qu85rj$1tmq$1@gioia.aioe.org>
In reply to#37982
Jonas,

> Hello Rudy, I can explain in short what it do

You misunderstood me I'm afraid.  I have NO interest in knowning what that 
code fragment of yours is for.  Absolutily none whatsoever.  Sorry (not 
really, but thats what I'm supposed to say :-) ).

All that I'm trying to do is make /you/ understand what the (your!) problem 
is.    And you do that by SIMPLIFYING THE PROBLEM.   Cut everything away 
that doesn't actually have to do anything with the problem itself and see if 
what you are left with still behaves in a way you can't figure out.

And ofcourse check that the variables you're using actually contain what you 
think they should.  Bridges, buildings, ships and others have been destroyed 
because of some bozo working on assumptions instead of facts.

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#37985

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-12-28 12:16 -0800
Message-ID<01ecacc6-d3a1-4a1e-a935-8c40837739bc@googlegroups.com>
In reply to#37984
Den lördag 28 december 2019 kl. 19:11:06 UTC+1 skrev R.Wieser:
> Jonas,
> 
> > Hello Rudy, I can explain in short what it do
> 
> You misunderstood me I'm afraid.  I have NO interest in knowning what that 
> code fragment of yours is for.  Absolutily none whatsoever.  Sorry (not 
> really, but thats what I'm supposed to say :-) ).
> 
> All that I'm trying to do is make /you/ understand what the (your!) problem 
> is.    And you do that by SIMPLIFYING THE PROBLEM.   Cut everything away 
> that doesn't actually have to do anything with the problem itself and see if 
> what you are left with still behaves in a way you can't figure out.
> 
> And ofcourse check that the variables you're using actually contain what you 
> think they should.  Bridges, buildings, ships and others have been destroyed 
> because of some bozo working on assumptions instead of facts.
> 
> Regards,
> Rudy Wieser

No i am more curious about how two code fragments that work perfectly on their own with a "single if" can behave differently when connected by an else if, i find that mysterious.

[toc] | [prev] | [next] | [standalone]


#37986

From"R.Wieser" <address@not.available>
Date2019-12-29 08:52 +0100
Message-ID<qu9m04$jup$1@gioia.aioe.org>
In reply to#37985
Jonas,

> No i am more curious about how two code fragments that work
> perfectly on their own with a "single if" can behave differently when
> connected by an else if, i find that mysterious.

Simplifying the problem will help you understand what actually happens.

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#37989

FromArno Welzel <usenet@arnowelzel.de>
Date2019-12-30 21:35 +0100
Message-ID<h6v5d0F3fmoU1@mid.individual.net>
In reply to#37985
Jonas Thörnvall:

> No i am more curious about how two code fragments that work perfectly
> on their own with a "single if" can behave differently when connected
> by an else if, i find that mysterious.

Example:

Assume a<b is true and also x>y is true.

if (a<b && x>y) {
  // will execute, since a<b is true and also x>y is true
}

if (a<b) {
  // will also execute, since a<b is true and no other condition
  // needs to be fulfilled
}

BUT BEWARE:

if (a<b && x>y) {
  // will execute, since a<b is true and also x>y is true
} else if (a<b) {
  // will NOT execute, since the first condition was already met
  // and this code path therefore will never be run
}

So the second fragemnt can also be read as:

if (a<b) {
  if (x>y) {
    // do something if a<b AND x>y
  } else {
    // do something else if a<b
  }
}

HTH

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#37990

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-12-30 12:42 -0800
Message-ID<4c56fca0-f6b1-420e-9a5a-0be3ffe1666f@googlegroups.com>
In reply to#37989
Den måndag 30 december 2019 kl. 21:35:50 UTC+1 skrev Arno Welzel:
> Jonas Thörnvall:
> 
> > No i am more curious about how two code fragments that work perfectly
> > on their own with a "single if" can behave differently when connected
> > by an else if, i find that mysterious.
> 
> Example:
> 
> Assume a<b is true and also x>y is true.
> 
> if (a<b && x>y) {
>   // will execute, since a<b is true and also x>y is true
> }
> 
> if (a<b) {
>   // will also execute, since a<b is true and no other condition
>   // needs to be fulfilled
> }
> 
> BUT BEWARE:
> 
> if (a<b && x>y) {
>   // will execute, since a<b is true and also x>y is true
> } else if (a<b) {
>   // will NOT execute, since the first condition was already met
>   // and this code path therefore will never be run
> }
> 
> So the second fragemnt can also be read as:
> 
> if (a<b) {
>   if (x>y) {
>     // do something if a<b AND x>y
>   } else {
>     // do something else if a<b
>   }
> }
> 
> HTH
> 
> -- 
> Arno Welzel
> https://arnowelzel.de

No but that is the point with the else if only one should be exectuted it is two cases. But something change in the if when changed to else if.

[toc] | [prev] | [next] | [standalone]


#37983

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2019-12-28 09:37 -0800
Message-ID<a94feae5-f0fe-4a0e-bb87-ce60d20695ff@googlegroups.com>
In reply to#37980
On Saturday, December 28, 2019 at 2:40:13 AM UTC-6, Jonas Thörnvall wrote:
> How can two if statements that work perfectly standalone as single statments.
> Make outcome that differs when you put in, an else if statement to only execute one of them. The below statements have the statement with heavier conditioning first so not the second statement gets executed. Because both satisfy the noneboolean condition.
> 
> I probably done some very no no thingy.
> 
>  	
> 
> if (EDITEND<SELEND && document.getElementById("lockedit").checked){
>   SELLENGTH=SELEND-SELSTART;
>   document.getElementById("SB").value=EDITSTART+SELLENGTH;
>   document.getElementById("EB").value=SELEND;
> } else if (EDITEND<SELEND){ document.getElementById("EB").value=SELEND;}

Your primary problem is that you don't understand what short-circuiting is:

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation>

Your second problem is that you make things too complicated for yourself. You should use variables:

```javascript
let elLockedit = document.getElementById("lockedit"),
    elSB = document.getElementById("SB"),
    elEB = document.getElementById("EB")

if (EDITEND < SELEND && elLockedit.checked) {
  SELLENGTH =  SELEND-SELSTART;
  elSB.value = EDITSTART + SELLENGTH;
  elEB.value = SELEND;
} else if (EDITEND < SELEND) {
    elEB.value = SELEND;
}
```

Third, don't use UPPERCASE variables unless they are constants. 

Finally, update your logic and change the order of evaluation of your first conditional:

```javascript
if (elLockedit.checked && EDITEND < SELEND) {

} else if (EDITEND < SELEND) {

}
```

[toc] | [prev] | [next] | [standalone]


#37987

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2019-12-29 11:43 +0100
Message-ID<2136104.ElGaqSPkdT@PointedEars.de>
In reply to#37983
Michael Haufe (TNO) wrote:

> ```javascript

This is not a Markdown document :)

> if (elLockedit.checked && EDITEND < SELEND) {
> 
> } else if (EDITEND < SELEND) {
> 
> }

DRY:

  const selectionEdited = (EDITEND < SELEND);

  if (elLockedit.checked && selectionEdited) {
    // …
  } else if (selectionEdited) {
    // …
  }

Which leads us to

  if (selectionEdited) {
    if (elLockedit.checked) {
      // …
    }
    else {
      // …
    }  
  }

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#37988

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2019-12-29 08:14 -0800
Message-ID<1afb2474-dd1a-4596-9d64-bc8468496808@googlegroups.com>
In reply to#37987
On Sunday, December 29, 2019 at 4:43:34 AM UTC-6, Thomas 'PointedEars' Lahn wrote:
> Michael Haufe (TNO) wrote:
> 
> > ```javascript
> 
> This is not a Markdown document :)

Yeah, but I prefer this form over putting '<script>' around examples as it avoids the tendency of some to assume that they could be dropped into a webpage as given; especially for non JavaScript examples. 

[toc] | [prev] | [next] | [standalone]


#38085

FromSoviet_Mario <SovietMario@CCCP.MIR>
Date2020-01-18 21:06 +0100
Message-ID<qvvogi$af3$1@dont-email.me>
In reply to#37980
On 28/12/19 09:40, Jonas Thörnvall wrote:
> How can two if statements that work perfectly standalone as single statments.
> Make outcome that differs when you put in, an else if statement to only execute one of them. The below statements have the statement with heavier conditioning first so not the second statement gets executed. Because both satisfy the noneboolean condition.
> 
> I probably done some very no no thingy.
> 
>   	
> 
> if (EDITEND<SELEND && document.getElementById("lockedit").checked){
>    SELLENGTH=SELEND-SELSTART;
>    document.getElementById("SB").value=EDITSTART+SELLENGTH;
>    document.getElementById("EB").value=SELEND;
> } else if (EDITEND<SELEND){ document.getElementById("EB").value=SELEND;}
> 

do you have the rock solid warranty that none of the 
statements/function calls MODIFIES the internal status of 
the objects probed ?
If somewhere sth gets changed by the inquiry, the order 
could become relevant.

a trivial example of what I mean

int q, k = 0;
if (k == 0) q = 0; else if (++ k != 0) q = 1;
if (++ k != 0) q = 0; else if (k == 0) q = 1;

the state of q depends on the order the two tests on k are 
executed.

maybe getElementById alters sth ... dunno ...


-- 
1) Resistere, resistere, resistere.
2) Se tutti pagano le tasse, le tasse le pagano tutti
Soviet_Mario - (aka Gatto_Vizzato)

[toc] | [prev] | [next] | [standalone]


#38087

FromSoviet_Mario <SovietMario@CCCP.MIR>
Date2020-01-19 01:22 +0100
Message-ID<r007fk$3cu$1@dont-email.me>
In reply to#38085
On 18/01/20 21:06, Soviet_Mario wrote:
> On 28/12/19 09:40, Jonas Thörnvall wrote:
>> How can two if statements that work perfectly standalone 
>> as single statments.
>> Make outcome that differs when you put in, an else if 
>> statement to only execute one of them. The below 
>> statements have the statement with heavier conditioning 
>> first so not the second statement gets executed. Because 
>> both satisfy the noneboolean condition.
>>
>> I probably done some very no no thingy.
>>
>>
>>
>> if (EDITEND<SELEND && 
>> document.getElementById("lockedit").checked){
>>    SELLENGTH=SELEND-SELSTART;
>>    document.getElementById("SB").value=EDITSTART+SELLENGTH;
>>    document.getElementById("EB").value=SELEND;
>> } else if (EDITEND<SELEND){ 
>> document.getElementById("EB").value=SELEND;}
>>
> 
> do you have the rock solid warranty that none of the 
> statements/function calls MODIFIES the internal status of 
> the objects probed ?
> If somewhere sth gets changed by the inquiry, the order 
> could become relevant.
> 
> a trivial example of what I mean
> 
> int q, k = 0;
> if (k == 0) q = 0; else if (++ k != 0) q = 1;
> if (++ k != 0) q = 0; else if (k == 0) q = 1;
> 
> the state of q depends on the order the two tests on k are 
> executed.
> 
> maybe getElementById alters sth ... dunno ...
> 
> 

sorry I am in the wrong NG :( I thought I was reading C++ NG 
... the syntax is similar at times


-- 
1) Resistere, resistere, resistere.
2) Se tutti pagano le tasse, le tasse le pagano tutti
Soviet_Mario - (aka Gatto_Vizzato)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web