Can you literally explain me like operators? (SOLVED) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you literally explain me like operators? (SOLVED)

ok so what i dont understand is why we do like a++ but answer is another. Ok let me explain with an example code. int pl = 43 pl 43 pl++ output is 44 (this isnt even a correct code lmao) can you explain me this??

25th Sep 2016, 8:23 PM
UMY Games
UMY Games - avatar
8 Answers
+ 2
The ++ increment operator is correct code. It is one of two unary operators and ++ simply adds 1 to the value of the numerical variable. Compared to the -- that subtracts 1. It is just a short and easy way to add 1 to the numerical variable (int a=5; a++ now equals 6). You really don't have to use ++ if you don't want to, but the other two options take longer expecially if you are writing a lot of code(see below for the other two ways). Also the reason you have to use ++ is because if you were to use a single +, the program will expect there to be another numberical value or variable to add to, and another numerical variable to hold the two values added. The program would think you wanted to do this: ?=a+?; and it would not know what to add to variable a, and would not have a declared variable assigned to the sum in order to save the new variable total in a memory location. Essentially just putting one +, instead of two ++, will cause a compiling/ sometype error in this situation. You could do this: a=a+1; which can be simplified to a+=1; which can be furthered simplified to a++; all three do the exact same thing and adds 1 to the numerical variable in this example. C# just offers some shorthand ways to write commonly performed expressions with less code, such as ++. As the old saying goes, the best code is the one with the least amount of code required to perform it's desired results. Thus ++ was created for us lazy programmers that can't bare to type a few extra keystrokes. Also if anyone anyone disagrees with my answer let me know. I am just a computer science major that thinks he knows everything (ha) and I am answering this question in between coding projects at 2 in the morning.
26th Sep 2016, 7:08 AM
Alex Wiggins
Alex Wiggins - avatar
+ 2
int pl=43; int a = 1; int b=1; a+= pl++; //out =44; pl=43; b+= ++pl; //out=45;
19th Oct 2016, 11:17 AM
Navid Tak
Navid Tak - avatar
+ 1
because we can. instead pl = pl +1; we use pl++; its shortes version and convencion in programing world
26th Sep 2016, 6:51 AM
Daniel Dudziak
Daniel Dudziak - avatar
0
I got it but why we use double plus.
26th Sep 2016, 4:31 AM
UMY Games
UMY Games - avatar
0
Because (+ +) is a syntax and is a contract in a programing language
26th Sep 2016, 10:07 PM
Navid Tak
Navid Tak - avatar
0
thanks for helping
27th Sep 2016, 4:43 AM
UMY Games
UMY Games - avatar
0
The code shows u that u r writing the short lines... Like u want to write pl=pl+1; But program allows you to use it as pl++; Meaning of both statement is same.
27th Sep 2016, 9:01 PM
Jai Verma
Jai Verma - avatar
0
Single + means a addition operation while double + means an increment. As similar single - means subtraction and double - means decrement.
27th Sep 2016, 9:03 PM
Jai Verma
Jai Verma - avatar