Prefix/Postfix
#1

I was reading a tutorial about the different type of loop structures (Link) and noticed something talking about prefix and postfix. The only difference it mentioned about the two was that it evaluated the operator before the variable and vice versa.

Код:
--myVariable //prefix
myVariable-- //postfix
Other than what they explained as the difference was, is there any other differences that would make it more efficient/useful in one set of conditions than in others?
Reply
#2

Nope. I think not. It is same using --something or something-- as far as I know
Reply
#3

No, those more differ in the usage than efficiency, if you check if --variable<10, that means variable will be 9 as you will be deducting it before the comparison, if you use variable--<10, variable will be 8 when loop stops as you first compare then deduct.
Reply
#4

Код:
new a = 0;          // a = 0
printf("%d", a++);  // prints a = "0", increase a +1
new b = 0;          // b = 0
printf("%d", ++b); // increase b + 1, prints b = "1"
I think nothing different to compare by efficiency, but that will be useful in function to write a fewer line for example, i increased variable and convert it to string.
Код:
++variable;
valstr(string, variable);
To:
Код:
valstr(string, ++variable);
Or vice versa:
Код:
valstr(string, variable);
variable++;
To:
Код:
valstr(string, variable++);
Sorry for my bad english.
Reply
#5

The only difference between prefix and postfix operators is in the result of these. In postfix, a temporary object is created, the operation is applied, and the temporary is returned: the object before the operation is applied. In prefix, the operation is applied and the fixed object is returned, without the use of a temporary: the object after the operation is applied.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)