+ 11

Why the answer is 19?

#include <iostream> using namespace std; int main() { int x=4; int y=++x + ++x + ++x; cout<<y; return 0; // answer is 19 } Can someone explain why is 19? I got 18. Here is how I did: x=x+1. 4+1=5 x=x+1. 5+1=6 x=x+1. 6+1=7 y=x+x+x. y=5+6+7=18 What am I doing wrong?

1st Aug 2019, 3:32 AM
Cristian Daraban
Cristian Daraban - avatar
9 Réponses
+ 2
Jacob Burgess I am guessing kms does not stand for kilometres?
2nd Aug 2019, 8:11 AM
Sonic
Sonic - avatar
+ 1
++x = 1+4 = 5 1+4 = 5 + ++x = 1+1+5 = 7 5+2 = 7 + ++x = 1+1+5 = 7 7+2 = 7 ans. 5+7+7 = 19 final ans. 19
6th Aug 2019, 12:10 PM
Yann Espuelas
Yann Espuelas - avatar
0
Answer = 5 + 5 + 9 = 19
7th Aug 2019, 6:50 AM
Pieter Rall
0
Hw these area available guys
13th Aug 2019, 9:47 AM
Ranjith Ranjith G S
0
It's purely compiler dependent
5th Sep 2019, 4:24 PM
[No Name]
[No Name] - avatar
- 1
x= x+1 => 4+1=5 x= x => 5 y= x +1 + x => 10 y=(x +1+ (x +1))+ 1 => y = 11 x =((( x + 1)+1)+1 ) + 1=> x = 8 y = 8 + 11 = 19
3rd Aug 2019, 5:52 PM
Reza Faghani (‫رضا‬‎)
Reza Faghani (‫رضا‬‎) - avatar