• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Flow Control

 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I have some problems about Flow Control :
1. In The Java Languange Specification 2nd Edition page 294-297 Section 14.13 The for Statement ....... there are statements that say :
If the ForInit part is not present, no action is taken (page 295 Section 14.13.1)
If the ForUpdate is not present, no action is taken (page 296 Section 14.13.2 and Section 14.13.3)
This is my code:
public class Loop
{
public static void main(String[] args)
{
int i = 0;
for ( ; i<10; i++) // ForInit is not present
{
System.out.print( " " + i );
}
}
}

If I run, this code I get : 0 1 2 3 4 5 6 7 8 9
And
public class Loop
{
public static void main(String[] args)
{
for ( int i = 0; i< 10 ; ) // ForUpdate is not present
{
System.out.print( " " + i );
}
}
}
If I run, this code I get : 0 0 0 0 .... until ???
According to the statements, actually I don't have result when I run these code. What is the meaning of "no action is taken" ?
2. In The Java Languange Specification 2nd Edition page 291-294 Section 14.11 The while Statement and 14.12 The do Statement ....... there are statements that say :
If execution of the Statement completes abruptly because of a continue with no Label, then the entire while statement is executed again (page 292 Section 14.11.1)
If execution of the Statement completes abruptly because of a continue with no Label, then the Expression is evaluated. Then there is a choice based on the
resulting :
- If the value is true, then the entire do statement is executed again
- If the value is false, no further action is taken and the do statement completes normally
(page 239 Section 14.12.1)
This my code :
public class Loop
{
public static void main(String[] args)
{
int i = 0;
while (i<10)
{
System.out.print(" " + i);
i++;
if ( i == 5 )
{
continue;
}
}
}
}

If I run this code, I get : 0 1 2 3 4 5 6 7 8 9
And
public class Loop
{
public static void main(String[] args)
{
int i = 0;
do
{
System.out.print(" " + i);
i++;
if (i==5)
{
continue;
}
}
while (i<10);
}
}
If I run this code, I get: 0 1 2 3 4 5 6 7 8 9
According to the statements, actually I don't get that result so what is the meaning of "the entire do statement is executed again" and "the entire while statement is executed again"?

thx
daniel
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. In The Java Languange Specification 2nd Edition page 294-297 Section 14.13 The for Statement ....... there are statements that say :
If the ForInit part is not present, no action is taken (page 295 Section 14.13.1)
If the ForUpdate is not present, no action is taken (page 296 Section 14.13.2 and Section 14.13.3)
This is my code:

If I run, this code I get : 0 0 0 0 .... until ???

[This message has been edited by marilyn murphy (edited September 22, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fisher,
1. If a portion of the for-loop expression is missing then it doesn't get executed so 'no action is taken'. Nothing happens in relation to the missing section.
In your first example, the initialization expression is missing. As one does not exist, there is no attempt to execute it and no error is raised. It doesn't present a problem as the variable 'i' used in the comparison and update portions is valid.
In your second example, there is nothing to update to nothing is executed and no error is raised. The value of 'i' never changes and the loop will go on and on since the condition 'i<10' will always be true.
In fact, the accepted idiom for creating a continuous loop is <code>for( ; ; )</code>. All three parts of the expression can be left out. No error occurs and any code in the for-block will repeat endlessly.
2. In both your examples, the loops do continue as expected. You are seeing the same output as the value of 'i' doesn't change ; it equals 5 and the loops are just testing for 'i<10' and printing 'i'.
Try changing the value of 'i' in the 'if' blocks, before you call 'continue' and see what happens. For example, if you change 'i' to '9' you should see 0 1 2 3 4 5 9. If you change 'i' to '11' you'll only see 0 1 2 3 4 5 as a value of '11' will return false for the loop condition of 'i<10'.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
marilyn murphy
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put the i++ after the 'continue', you will see its effect better.

Since the continue interrupts the loop, i never gets incremented beyond 5 and an endless loop results.
output: 0 1 2 3 4 5 5 5 ...
Same thing happens with the do/while loop.
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answers
But I am still confuse about the statement "the entire while/do statement is executed again". Compare with "for statement" if there is continue with no label. In JLS page 296 Section 14.13.3 says "Second, another for iteration step is performed". Are there differences between while/do and for statement?
thanks
daniel
reply
    Bookmark Topic Watch Topic
  • New Topic