• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Increment in Ternary Expression

 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



here y++ is a post increment. why is it giving output as 2 like a pre-increment?
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 15743
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that you're attempting to print y, not x.
 
Ranch Hand
Posts: 51
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80874
506
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic