By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Using variables inside for loop

P: 14
Hi,

I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class Exc
  3. {
  4.  Exc ()
  5.  {
  6.   System.out.println ("Haribol");
  7.  }
  8.  
  9.  static public void main ( final String [] args )
  10.  {
  11.   for ( int i = 0 ; i < 4 ; ++ i )
  12.   {
  13.    Exc e = new Exc ();
  14.   }
  15.  }
  16. }
  17.  
There is a problem with this code. Let me first tell you my intentions. I want to use the variable 'e' ( of type 'Exc' ) only inside the for loop. That is, I want to restrict its scope to the for loop. So I have declared it inside the loop. But I also want to initialize it with a new object of type 'Exc'. So I have written a declaration cum initializer statement. But since the statement is inside the for loop, it gets executed multiple number of times ( 4 times to be precise ). My intention was to have it executed only once.

There is one solution to this problem which is to use a counter or a sentinel that will signal the number of times that the loop has executed. Fortunately here, the for loop is using a 'built-in' counter namely 'i'. So the code now becomes:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class Exc
  3. {
  4.  Exc ()
  5.  {
  6.   System.out.println ("Haribol");
  7.  }
  8.  
  9.  static public void main ( final String [] args )
  10.  {
  11.   for ( int i = 0 ; i < 4 ; ++ i )
  12.   {
  13.    Exc e;
  14.    if ( i == 0 ) e = new Exc ();
  15.   }
  16.  }
  17. }
  18.  
Another solution for this would be to enclose the for loop inside a code block delimited by '{' and '}' and declare the variable 'e' outside the for loop but inside the code block. The variable 'e' will also be initialized when it's declared. Here's what I mean:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class Exc
  3. {
  4.  Exc ()
  5.  {
  6.   System.out.println ("Haribol");
  7.  }
  8.  
  9.  static public void main ( final String [] args )
  10.  {
  11.   {
  12.    Exc e = new Exc () ;
  13.  
  14.    for ( int i = 0 ; i < 4 ; ++ i )
  15.    {
  16.     //call some methods on 'e'
  17.    }
  18.   }
  19.  }
  20. }
  21.  
This way the variable 'e' will have its scope restricted to the for loop and also be 'initialized' only once.

But these solutions are somewhat artificial. Their design intentions are not immediately clear. Are there any elegant solutions for this design problem - something that is naturally expressed by the Java language ?
Jan 27 '09 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
Your second 'solution' fails because evertime the loop body is executed a new variable 'e' is defined and it will only be initialized when i == 0. Imho your third solution is fine because you don't create a new scope { ... } for nothing. It'd catch my eye.

kind regards,

Jos
Jan 27 '09 #2

jkmyoung
Expert 100+
P: 2,057
How about:
Expand|Select|Wrap|Line Numbers
  1. for ( int i = 0, Exc e = new Exc () ; i < 4 ; ++ i ) 
  2. {
  3. // operations on e. 
  4. }
Jan 27 '09 #3

Expert 10K+
P: 11,448
@jkmyoung
You can't do that: an initialization list is limited to only one type. You try to introduce two types here: an int and an Exc.

kind regards,

Jos
Jan 27 '09 #4

P: 14
Thank you, for your kind reply.

Warm Regards,
Ganesh
Jan 28 '09 #5

BSCode266
P: 38
Do you really want to go through the for loop 4 times? If not you could always use a break; And i don't see whats wrong with the if(i==0) if you are only going to use it once.
Jan 31 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.