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


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

Why i++ can not be used in conditional operator?

Started byXue Feng <xf.lotus@yahoo.com>
First post2019-08-16 11:01 +0800
Last post2019-08-20 11:03 -0700
Articles 3 — 3 participants

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


Contents

  Why i++ can not be used in conditional operator? Xue Feng <xf.lotus@yahoo.com> - 2019-08-16 11:01 +0800
    Re: Why i++ can not be used in conditional operator? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2019-08-16 09:45 +0200
      Re: Why i++ can not be used in conditional operator? taqmcg@gmail.com - 2019-08-20 11:03 -0700

#39103 — Why i++ can not be used in conditional operator?

FromXue Feng <xf.lotus@yahoo.com>
Date2019-08-16 11:01 +0800
SubjectWhy i++ can not be used in conditional operator?
Message-ID<qj56aa$6jg$1@gioia.aioe.org>
Hi all,
   In C, the expression 'inc ? i++ : i--' is vaild, and can be accepted 
by compiler. But in Java the same expression will cause the compiler to 
issue errors. I wonder the 'i++' (or 'i--') is rather a statement than 
an expression, as such can not return a value. Is that true?

[toc] | [next] | [standalone]


#39104

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2019-08-16 09:45 +0200
Message-ID<qj5mus$8u9$1@news4.open-news-network.org>
In reply to#39103
Am 16.08.19 um 05:01 schrieb Xue Feng:
> But in Java the same expression will cause the compiler to 
> issue errors. I wonder the 'i++' (or 'i--') is rather a statement than 
> an expression, as such can not return a value. Is that true?

You are on the right way. It is the other way around.

i++ is an /expression/ and returns a value, namely i before the 
increment since you used the postfix operators. The conditional operator 
is also always an expression that returns one of the values. But now you 
have a value followed by a semicolon, and that is not allowed.
AFAIK only results of function calls or assignment operators can be 
discarded.

However, the desired result could achieved in one line like that:
   i += inc ? 1 : -1;


Marcel

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


#39117

Fromtaqmcg@gmail.com
Date2019-08-20 11:03 -0700
Message-ID<e7d22b5d-e830-460f-9c9b-a3267bdbd1f8@googlegroups.com>
In reply to#39104
On Friday, August 16, 2019 at 3:45:42 AM UTC-4, Marcel Mueller wrote:
> Am 16.08.19 um 05:01 schrieb Xue Feng:
> > But in Java the same expression will cause the compiler to 
> > issue errors. I wonder the 'i++' (or 'i--') is rather a statement than 
> > an expression, as such can not return a value. Is that true?
> 
> You are on the right way. It is the other way around.
> 
> i++ is an /expression/ and returns a value, namely i before the 
> increment since you used the postfix operators. The conditional operator 
> is also always an expression that returns one of the values. But now you 
> have a value followed by a semicolon, and that is not allowed.
> AFAIK only results of function calls or assignment operators can be 
> discarded.
> 
> However, the desired result could achieved in one line like that:
>    i += inc ? 1 : -1;
> 
> 
> Marcel

I don't think there is any problem here...   The following code works for me.

public class Test {
    public static void main(String[] args) {
	boolean b = false;
	int     i = 1;
	int     j;
	
	System.out.println("I starts out:"+i);
	j = b ? i++: i--;
	System.out.println("J is now:"+j);
	System.out.println("I is now:"+i);
    }
}
and finishes with j=1 and i=0.

While I would never do this (in fact I never use the ++ or -- operators, only the += and -= operators) Java seems happy enough with it.

Perhaps the OP's issue was with the much stricter typing of Java.  E.g., maybe his 'inc' was an integer type and not a boolean.

    Regards,
    Tom McGlynn


[toc] | [prev] | [standalone]


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


csiph-web