a=7; p= 0; solve: p+=(++a)-(--a); plz explain and solve | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

a=7; p= 0; solve: p+=(++a)-(--a); plz explain and solve

answer is 0 maybe but i cant understand plz explain. java expressions question.

25th Sep 2020, 1:15 PM
Ali Xd Rocks
Ali Xd Rocks - avatar
2 Answers
+ 4
I think you have not noticed answer properly . Output should be 1 instead of 0 . I tried in c cpp and java it giving 1 not 0. public class Program { public static void main(String[] args) { int a=7,p=0; p=p+ (++a)-(--a); System .out.println (p); } } You intilize a=7 and p =0 When u calculate expression it will calculate like this Her me u used pre increment and pre decrement but pre increment have highest priority so first a will increase by 1so a=8 and 8 will use in whole expression but after tha (--a) here first value will be decrease so it will again be 7 So p=p+ (++a) -(--a) p=0+ 8 -7 p=1
25th Sep 2020, 1:28 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
In C & C++ this is considered as an example of sequence point issue. Order of execution is unspecified, which leads to unpredictable (and most probably undesired) output. You will see a sequence point warning if you run this code in Code Playground. (Edit) Java doesn't have this symptom.
25th Sep 2020, 2:20 PM
Ipang