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


Groups > comp.lang.java.programmer > #38710 > unrolled thread

value of Math.abs(a), int a=null;

Started byAkshay Goel <goelakshay001@gmail.com>
First post2019-01-22 08:09 -0800
Last post2019-01-22 11:32 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  value of Math.abs(a), int a=null; Akshay Goel <goelakshay001@gmail.com> - 2019-01-22 08:09 -0800
    Re: value of Math.abs(a), int a=null; Arne Vajhøj <arne@vajhoej.dk> - 2019-01-22 11:20 -0500
    Re: value of Math.abs(a), int a=null; Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-01-22 11:32 -0500

#38710 — value of Math.abs(a), int a=null;

FromAkshay Goel <goelakshay001@gmail.com>
Date2019-01-22 08:09 -0800
Subjectvalue of Math.abs(a), int a=null;
Message-ID<80e0e6bf-ef5a-4f3f-adf4-b5de4c6696ef@googlegroups.com>
Hi all,

In below code, how is the if statement working?



Integer closest = null;
		for(int i = 0; i < n; i++) {
			int current = in.nextInt();
			if(closest == null || Math.abs(closest) > Math.abs(current) 
					|| Math.abs(closest) == Math.abs(current) && closest < current) closest = current;
		}
		System.out.println(closest);

[toc] | [next] | [standalone]


#38711

FromArne Vajhøj <arne@vajhoej.dk>
Date2019-01-22 11:20 -0500
Message-ID<q27fs5$17rp$1@gioia.aioe.org>
In reply to#38710
On 1/22/2019 11:09 AM, Akshay Goel wrote:
> In below code, how is the if statement working?
> 
> Integer closest = null;
> 		for(int i = 0; i < n; i++) {
> 			int current = in.nextInt();
> 			if(closest == null || Math.abs(closest) > Math.abs(current)
> 					|| Math.abs(closest) == Math.abs(current) && closest < current) closest = current;
> 		}
> 		System.out.println(closest);

|| is a short circuiting operator.

So if closest == null is true then it skip the other two OR
conditions and do closest = current..

Arne

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


#38712

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2019-01-22 11:32 -0500
Message-ID<q27gjr$v3t$1@dont-email.me>
In reply to#38710
On 1/22/2019 11:09 AM, Akshay Goel wrote:
> Hi all,
> 
> In below code, how is the if statement working?
> 
> 
> 
> Integer closest = null;
> 		for(int i = 0; i < n; i++) {
> 			int current = in.nextInt();
> 			if(closest == null || Math.abs(closest) > Math.abs(current)
> 					|| Math.abs(closest) == Math.abs(current) && closest < current) closest = current;
> 		}
> 		System.out.println(closest);

     It tests a combination of three conditions, one of which is
itself a compound expression of two conditions.  The combination
is true if

	closest is still `null'
OR
	|closest| > |current| (closest known to be non-`null')
OR
	the compound expression is true (see below)

     The compound expression has two conditions, and is true if

	|closest| == |current| (closest known to be non-`null')
     AND
	closest < current (closest known to be non-`null')

     Both the || and the && operators evaluate their operands in left-
to-right order, stopping as soon as the outcome is known.  For example,
if closest is `null' no further evaluations or tests are made, since
the first test has come up `true' and therefore the total outcome is
`true' no matter how the remaining conditions turn out.  That's why
the "known to be" remarks above hold: If closest is `null' their
conditions are never evaluated, hence if they're evaluated it must
be the case that closest is non-`null'.

-- 
esosman@comcast-dot-net.invalid
Seven hundred twenty-nine days to go.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web