<< Chapter < Page Chapter >> Page >

The program in Listing 3 uses both the prefix and postfix forms of the increment operator. (This discussion also applies to the prefix and postfix forms of the decrement operator.)

Outside of another expression

If the increment operator is applied to a variable outside of another expression, as is the case with ++w and x++ in Listing 3 , it doesn't matter whether it is of the prefix or postfix form. The end result will the same in both cases. The value will have been incremented thenext time the variable is referenced. (This is also the case with the use of the increment operator in the adjustment clause of the for loop in Listing 2 .)

Inside of another expression

However, if the increment operator is applied to a variable inside another expression, as is the case with y++ and ++z in Listing 3 , the end result may depend on whether the prefix or postfix form is used.

Rules

Here is a pair of rules that govern such behavior:

  • If the ++ appears after the operand, as in y++ , the value of the operand is first used to evaluate the expression, and then itis immediately incremented. Therefore, y++ is displayed as 500 in Listing 3 . Immediately thereafter, y is displayed as 501 in Listing 3 .
  • If the ++ appears before the operand, as in ++z , the value of the operand is incremented before it is used to evaluate theexpression. Therefore, ++z is displayed as 5001 by the code in Listing 3 . Similarly z is also displayed as 5001 in Listing 3 .

And the answer to the question is...

C. 6 51 500 501 5001 5001

Run the program

I encourage you to run the programs that I have presented in this lesson to confirm that you get the same results. Copy the code for each program into yourIDE. Then compile and run each program. Experiment with the code, making changes, and observing the results of your changes. Makecertain that you can explain why your changes behave as they do.

Miscellaneous

This section contains a variety of miscellaneous materials.

Housekeeping material
  • Module name: Language Features, Increment and Decrement Operators
  • File: Hs01008.htm
  • Revised: 10/03/15
  • Keywords:
    • Advanced placement
    • AP Computer Science
    • Java subset
    • homeschool
    • increment operator
    • decrement operator
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDFfile for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware thatsome of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.comshowing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, pleasebe aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX, and have noaffiliation with the College Board .

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ap computer science a, clarification of the java subset. OpenStax CNX. Oct 03, 2015 Download for free at https://legacy.cnx.org/content/col11279/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ap computer science a, clarification of the java subset' conversation and receive update notifications?

Ask