Can someone explain the output of the program in step by step manner (i got the output 20..however answer is 25) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain the output of the program in step by step manner (i got the output 20..however answer is 25)

public class Program { public static void main(String[] args) { int x = 5; x=x++*2+3*--x; System.out.println(x); } }

15th Jul 2017, 1:18 PM
Stephen george
1 Answer
+ 5
The second line goes: x = x++*2 + 3*--x; (while initially x was assigned 5) Let's break it down to two parts, P1 and P2: x = P1 + P2 {P1=x++*2, P2=3*--x} What happens in P1: P1 = x*2 and after that, x gets incremented by 1. So P1 = 10 and x = 6 What happens in P2: P2 = 3*--x, so x gets decremented by 1 before being taken to multiplication So x = 6-1 = 5, thus P2 = 3*5 = 15 Finally, x gets assigned to P1 + P2, so 10 + 15 = 25
15th Jul 2017, 4:02 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar