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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Increment in Ternary Expression

Greenhorn
Posts: 16
1

here y++ is a post increment. why is it giving output as 2 like a pre-increment?

Saloon Keeper
Posts: 8576
161
It doesn't print anything, because line 4 of that code is invalid. Please post the real code.

Stephan van Hulst
Saloon Keeper
Posts: 8576
161
• 1
Note that you're attempting to print y, not x.

Ranch Hand
Posts: 51
2
• 1

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: 115
11
• 1

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: 58295
178
• 1
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: 58295
178
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

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
Marshal
Posts: 23435
138

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: 11338
177

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.