Using variables inside for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgurukrupagmailcom
    New Member
    • Apr 2008
    • 14

    Using variables inside for loop

    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:
    Code:
     
    class Exc
    {
     Exc ()
     {
      System.out.println ("Haribol");
     }
     
     static public void main ( final String [] args )
     {
      for ( int i = 0 ; i < 4 ; ++ i )
      {
       Exc e = new Exc ();
      }
     }
    }
    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:
    Code:
     
    class Exc
    {
     Exc ()
     {
      System.out.println ("Haribol");
     }
     
     static public void main ( final String [] args )
     {
      for ( int i = 0 ; i < 4 ; ++ i )
      {
       Exc e;
       if ( i == 0 ) e = new Exc ();
      }
     }
    }
    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:
    Code:
     
    class Exc
    {
     Exc ()
     {
      System.out.println ("Haribol");
     }
     
     static public void main ( final String [] args )
     {
      {
       Exc e = new Exc () ;
     
       for ( int i = 0 ; i < 4 ; ++ i )
       {
        //call some methods on 'e'
       }
      }
     }
    }
    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 ?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      How about:
      Code:
      for ( int i = 0, Exc e = new Exc () ; i < 4 ; ++ i ) 
      {
      // operations on e. 
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by jkmyoung
        How about:
        Code:
        for ( int i = 0, Exc e = new Exc () ; i < 4 ; ++ i ) 
        {
        // operations on e. 
        }
        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

        Comment

        • sgurukrupagmailcom
          New Member
          • Apr 2008
          • 14

          #5
          Thank you, for your kind reply.

          Warm Regards,
          Ganesh

          Comment

          • BSCode266
            New Member
            • Jan 2007
            • 38

            #6
            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.

            Comment

            Working...