Java Reference
In-Depth Information
Note that one can define and initialize several variables in the first part of the for
statement. However, all the variables that are defined must be of the same type and the
variables are separated by commas. Remember that the first part of the for statement
is executed exactly once when the for loop starts executing. The second part of the for
statement must contain a single condition (if the condition is missing, then true is the
default value). The third part of the for statement can contain several commands separated
by commas. The third part of the for loop is always executed after the body of the for
loop is executed. Note that the first and/or the third part of the for statement can also be
missing. In particular, the following for statement defines an infinite loop: for(;;)
Thefirstouter for statement first initializes the variables numSpaces and numStars .
Themiddlepartofthe for statement tells us that we will keep going while the variable
numberOfStars is greater or equal to zero. The third part of the for statement tells us
that at the end of every iteration we will increase the number of stars by 2 and decrease
the number of spaces by 2. The inner for loop simply prints the required number of stars
and spaces. The second outer for loop is similar. The difference is that now the variable
numStars is initially set to size -2 and the variable numSpaces is initially set to 2. After
the body of the loop executes, the variable numSpaces will now be increased by two, while
the variable numStars will now be decreased by two. The body of the for loop will keep
executing while the number of stars to be printed is a positive number, that is, there are
stars to be printed.
Adding several counter variables to a single for loop is not uncommon. For example,
you may count the number of laps you have run and the number of times your shoe unties.
For example, you may want to keep running until you complete 10 laps or your shoe unties
3 times. In this case, the for statement may be: for( laps = 0, unties = 0; laps <10
&& unties < 3 ; laps++)
. Note that several conditions can be combined in the
middle part of a for loop using binary operators. However, the middle part of a for loop
must still be a single condition.
3.5 The Modulus Operation
In this last section of the chapter, we will cover the remainder (a.k.a., modulus )operation.
In math, when two integers are divided, there is a remainder. For example, when the number
25 is divided by the number 3, the result is 8 and the remainder is 1. In Java, the operator
% is used to represent the remainder operator. For example, 25%3 will give us the number
1. Note that 25/3 is equal to 8 because Java implements integer division. In other words,
25 = (25/3)*3+1. Therefore, another way to think about the remainder operation is to ask,
what is the difference to the closest number that is equal to or less than the dividend and is
divisible by the divisor. In other words, writing n%a is equivalent to writing n - (n/a)*a .
In order to demonstrate how the modulus operation works, let us create a simple ap-
plication that converts a decimal number into a hexadecimal number. The program will
take a number as input and will keep dividing the number by 16. Every time the division
is performed, the remainder will be saved as a character in the String variable result .If
Search WWH ::

Custom Search