Web host sites - Chapter 4: Making the Most of Variables and
Chapter 4: Making the Most of Variables and Their Values 97 A run of the code in Figure 4-12 is shown in Figure 4-13. Compare Figure 4-13 with the run in Figure 4-11: With preincrement in Figure 4-11, the second number is 29. With postincrement in Figure 4-13, the second number is 28. In Figure 4-13, the number 29 doesn t show up on-screen until the end of the run, when the computer executes one last out.println (numberOfBunnies). Figure 4-13: A run of the code in Figure 4-12. Are you trying to decide between using preincrement or postincrement? Try no longer. Most programmers use postincrement. In a typical Java program, you often see things like numberOfBunnies++. You seldom see things like ++numberOfBunnies. In addition to preincrement and postincrement, Java has two operators that use –. These operators are called predecrement and postdecrement. With predecrement (–numberOfBunnies), the computer subtracts 1 from the variable s value before the variable is used in the rest of the statement. With postdecrement (numberOfBunnies–), the computer subtracts 1 from the variable s value after the variable is used in the rest of the statement. Instead of writing ++numberOfBunnies, you could achieve the same effect by writing numberOfBunnies = numberOfBunnies + 1. So some people conclude that Java s ++and –operators are for saving keystrokes to keep those poor fingers from overworking themselves. This is entirely incorrect. The best reason for using ++is to avoid the inefficient and error-prone practice of writing the same variable name, such as numberOfBunnies, twice in the same statement. If you write numberOfBunniesonly once (as you do when you use ++or –), the computer has to figure out what numberOfBunniesmeans only once. On top of that, when you write numberOfBunniesonly once, you have only one chance (instead of two
We are the cheapest Catholic web hosting provider, check Catholic Web Hosting services and make sure of alone.