Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 5. Control Statements: Part 1 > Increment and Decrement Operators

5.12. Increment and Decrement Operators

C# provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator, --, respectively, which are summarized in Fig. 5.14. An application can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c = c + 1 or c += 1. An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment operator or prefix decrement operator, respectively. An increment or decrement operator that is postfixed to (placed after) a variable is referred to as the postfix increment operator or postfix decrement operator, respectively.

Fig. 5.14. Increment and decrement operators.
OperatorCalledSample expressionExplanation
++prefix increment++aIncrements a by 1, then uses the new value of a in the expression in which a resides.
++postfix incrementa++Uses the current value of a in the expression in which a resides, then increments a by 1.
--prefix decrement--bDecrements b by 1, then uses the new value of b in the expression in which b resides.
--postfix decrementb--Uses the current value of b in the expression in which b resides, then decrements b by 1.



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint