Postfix and Prefix Increment and Decrement Operators
Conceptually closely related to compound assignment with the +
and -
binary operators are the postfix and prefix increment and decrement operators. In essence, they abbreviate simple operators as “plus one” and “minus one” into succinct syntax, respectively as ++
and --
. One common use can be found in the context of for
loops, which are discussed in the Chapter 7, “Simple Control Flow.” Here is the typical pattern to create a loop that will execute the loop body 10 times:
for (int i = 0; i < 10; i++){ // Use integer value i if desired.}
A for
loop consists of four parts: an initializer (here int i = 0
), a condition (i < 10
), an iterator (i++
), and a loop body (the part in between curly braces). ...
Get C# 4.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.