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


Groups > comp.lang.javascript > #37989

Re: Logical conundrums if () else if()

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Arno Welzel <usenet@arnowelzel.de>
Newsgroups comp.lang.javascript
Subject Re: Logical conundrums if () else if()
Date Mon, 30 Dec 2019 21:35:41 +0100
Lines 43
Message-ID <h6v5d0F3fmoU1@mid.individual.net> (permalink)
References <cd84bfc2-63ac-4a4f-bd76-9b144035b9ba@googlegroups.com> <qu7drc$frb$1@gioia.aioe.org> <0d26314f-6691-4428-90c9-b6a83828f776@googlegroups.com> <qu85rj$1tmq$1@gioia.aioe.org> <01ecacc6-d3a1-4a1e-a935-8c40837739bc@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace individual.net N9KeyvU79/NN7lb6A1EDEAZ7KecTFdQo5Iwl2S9FSPTxBeu8ZZ
Cancel-Lock sha1:CuukHeJI4JNlp52SZeEreQQiqpU=
In-Reply-To <01ecacc6-d3a1-4a1e-a935-8c40837739bc@googlegroups.com>
Xref csiph.com comp.lang.javascript:37989

Show key headers only | View raw


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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web