• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

what's the continue functionality in do/while loop?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in scjp 310-035 QUESTION 63.
Given:
1. public class Alpha1 {
2. public static void main( String[] args ) {
3. boolean flag; int i=0;
5. do {
6. flag = false;
7. System.out.println( i++ );
8. flag = i < 10;
9. continue;
10. } while ( (flag)? true:false );
11. }
12. }

i only for sure the use of 'continue' in for loop, but in do/while, it seems has no effect. any guide appreciate!
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leon,

You won't see the effect by placing the continue at the end of the do/while loop. Try adding some conditions like this:



Joyce
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now thats what I call redundant.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Garrett,

while ( (flag)? true:false );
Now thats what I call redundant.


You'll tend to see this kind of funny code in mock questions. The purpose is to test our understanding of Java syntax, not on good programming practice.

Joyce
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, what does the "while ( (flag)? true:false );" mean? I'm looking at K&B 1.4, under the control section but can't seem to find an example that explains the "?" question mark, the ":" seem like an assertion to me. What is the "while" trying to compare?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Conditional Operator
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried setting "false" to "true" and the loop ran 10 times. This I can understand. But why is it that the loop can still go through 10 times with "flag" set to "false":



"flag" was set to false initially and the conditional operator still assign "false" to "flag".
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


flag = false;
System.out.println( i++ );
flag = i < 10; //You reset flag value base on i.

 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Wise!
 
reply
    Bookmark Topic Watch Topic
  • New Topic