Can anyone help me with this question output am getting confused | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone help me with this question output am getting confused

Int x =10; System.out.println (x++); System.out.println (x++); System.out.println (x--); System.out.println (--x)

19th Nov 2021, 6:48 AM
Julieth
Julieth - avatar
5 Answers
+ 1
1) : Post increment x++ : first value is returned or used there after then it increments.. so first it will print 10 then it increments x+=1 => x=11 2) : Again same happens so prints 11 and then x incremented so x=12 3) : Post decrement x-- works same first it uses value then decrements so prints 12 then decrements, x=x-1=>x=11 4): Pre -decrement operator --x : here first it decrements value then uses its value so x=x-1=>x=10, then it prints value so output is 10 for this. (Pre-increments works same instead increments instead of decrement) So output is 10 11 12 10 Hope it clears.. Search bar will provide a lot of similar answres if you want more information... Hope it helps....
19th Nov 2021, 6:57 AM
Jayakrishna 🇮🇳
+ 4
First you should learn basics of increment and decrements then u can easily solve these questions. Always in the expression pre increment or pre decrement will be evaluate the. Post increment or decrement Here in first line x++ this is post increment so i told pre increment will be solve so here first x value will be printed 10 then it will increase. After printing 10 now x++ will increase by one so current value of x is 11 Then again same things so 11 will be print then value increasing by 1 so current value will be 12 Again value will be print and in third statement here is x-- so first value will be print 12 then it will decrease by 1 Then in fourth Statement there is --x which ia pre decrement so first value will be decrease so now this time 10 will be print so final output will be 10 11 12 10
19th Nov 2021, 6:56 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Can you please tag Java instead of 'girlscode'? Java is the language that is relevant to your snippet. It's important to use proper tags ... https://code.sololearn.com/W3uiji9X28C1/?ref=app
19th Nov 2021, 6:52 AM
Ipang
+ 2
Thank you so much guysJayakrishna🇮🇳 and♨️A.S. Raghuvanshi♨️ you really helped me so much and I understood very well.
20th Nov 2021, 8:00 PM
Julieth
Julieth - avatar
+ 1
I see nothing wrong with the tag#girlscode ...it could be much better if you help me with the output of the problem above Ipang
20th Nov 2021, 8:06 PM
Julieth
Julieth - avatar