Adding in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Adding in C++

What's the difference between a = a+1 and a += 1? I noticed when I tried using a+2 in a loop, it responded with a timeout error.

14th Feb 2019, 10:35 PM
Daniel Cooper
Daniel Cooper - avatar
3 Answers
+ 6
a = a +1 is the same as a += 1 and still the same as a++ however if you use a+2 you bound to get a timeout error since value of "a" is never changed in that expression so use a = a+1, a+=1 or just simply a++ hope this help you
14th Feb 2019, 10:40 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
+ 3
Ok, so that makes sense So I'd have to do a = a+2. which could be shortened with a+=2. Thanks Mind To Machine 💻🕆
15th Feb 2019, 12:18 AM
Daniel Cooper
Daniel Cooper - avatar
+ 2
anytime Daniel Cooper 😊😎😉
15th Feb 2019, 1:30 AM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar