Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Increment in Ternary Expression  RSS feed

 
Prathyu Krishna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



here y++ is a post increment. why is it giving output as 2 like a pre-increment?
 
Stephan van Hulst
Saloon Keeper
Posts: 7505
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't print anything, because line 4 of that code is invalid. Please post the real code.
 
Stephan van Hulst
Saloon Keeper
Posts: 7505
135
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you're attempting to print y, not x.
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final int x= y<10 ? y++ : z++;

This line means if y is less than 10, increment y by 1 and assign it to x, else if y is more or equal 10 increment z by 1.

In your program y is less than 10 and y is incremented first and then assigned to x.

From this we know that after condition ?: y++(or could be method call) is executed
 
João Victor Gomes
Ranch Hand
Posts: 110
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spencers wrote:
final int x= y<10 ? y++ : z++;

This line means if y is less than 10, increment y by 1 and assign it to x, else if y is more or equal 10 increment z by 1.

In your program y is less than 10 and y is incremented first and then assigned to x.


Actually, the original value of y is assigned to x, since a post-increment operator is being used.
So, the initial value of y is 1, then y is less than 10, which means that the expression y++ will be evaluated. The original value is returned (1), and the y value is incremented.

At the end of the program, the values will be: x = 1, y = 2, z = 1

If you use the pre-increment operator instead, then the value is incremented first, and the new value is returned, like this:

Now, x = 2, y = 2, z = 1
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such code comes from. It must be fun writing Certs books, because you are allowed to write the sort of code which would get you a P45 anywhere else
You are in the realms of
i = i++;
which as we all know has no effect. In fact, that sort of code is discussed so often that we have an FAQ about it. But your code is slightly different because what you are assigning to is not y or z. So the increment is implemented and retained. But because you have a side‑effect inside another expression, as MS told you, only one of the increments is actually executed. You will either get (2, 1) or (1, 2) printed for any value of the number literal in line 3. Since you haven't posted a lot, I shall add code tags to your post, and you can see how much better it looks
 
Campbell Ritchie
Marshal
Posts: 54909
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everybody in UK knows what a P45 is; it is a certificate of tax paid given an employee when they leave a job . . . or when they are asked to leave. So, “being given a P45,” often means being dismissed.
 
Prathyu Krishna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1.int x=0;
2. x++;
3. System.out.println(x); // outputs 1



1. int x=0;
2. ++x;
3. System.out.println(x); //outputs 1
  
Can you walk me through the flow of increment and decrement operators in various situations?
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathyu Chandra wrote:
Can you walk me through the flow of increment and decrement operators in various situations?


The "x++" and "++x" expressions, by themselves, are functionally the same. It doesn't matter that it is a pre or post increment, as the expressions. either "x++" or "++x" are not used anywhere.

Now, if you did something like this, then you will see a difference.

Henry
 
Roel De Nijs
Sheriff
Posts: 11302
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathyu Krishna wrote:here y++ is a post increment. why is it giving output as 2 like a pre-increment?

First of all, your code snippet will not compile since a String concatenation operator is missing in the println() statement. Secondly you should print the value of x as well to see what's going on. So this line will print 1,2,1If you want to know what's happening behind the scenes, you must definitely read this excellent topic.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!