Java Reference
In-Depth Information
for(i=0;i>=0;i++)
{}
2.5.3 Loop equivalence to universal while structures
As mentioned earlier, the three loop structures in Java are all equivalent to a
universal while structure. These different loop syntaxes are provided to help
programmers quickly write code.
for(instructionInit; booleanCondition; instructionUpdate) block instruction;
instructionInit;
while (booleanCondition)
block instruction3; instructionUpdate;
2.5.4 Breaking loops at any time with break
We can voluntarily escape loops at any time by using the keyword break . This
special instruction is useful for example when we ask users to input any given
number of data.
2.5.5 Loops and program termination
Consider the following sequence
u i } i of integers numbers as follows:
u n +1 = u n / 2
{
if n is even,
,
3 u n + 1
otherwise.
initialized for any given u 0 N
.
For example, let u 0 = 14. Then u 1 = 14, u 2 =7, u 3 = 22, u 4 = 11, u 5 = 34,
u 6 = 17, u 7 = 52, u 8 = 26, u 9 = 13, u 10 = 40, u 11 = 20, u 12 = 10, u 13 =5,
u 14 = 16, u 15 =8, u 16 =4, u 17 =2, u 18 = 1. Once 1 is reached the sequence
cycles 1 , 4 , 2 , 1 , 4 , 2 ... . It is conjectured but not yet proved that for any u 0
1
the sequence reaches in a finite step number 1. We can numerically check that
this conjecture holds for a given number u 0 = n using the following do loop
structure:
Program 2.9 Syracuse's conjecture
do {
if ( ( n%2)==0)
 
 
Search WWH ::




Custom Search