user2037720
user2037720

Reputation: 93

For Loop Not Working?

public class twoTimes 

{

     public static void main(String[] args) 
     {

        for ( int i=1; i<11; i++);
        {
            System.out.println("count is" + i); 
        }


      }
}

When I try to display i, java is not able to initialise the variable?

Upvotes: 0

Views: 154

Answers (5)

Happy
Happy

Reputation: 169

public class twoTimes

{

 public static void main(String[] args) 
 {

    for ( int i=1; i<11; i++)//; <----- Due to this it is not working
    {
        System.out.println("count is" + i); 
    }


  }

}

Upvotes: 0

Felipe Mosso
Felipe Mosso

Reputation: 3927

It does not have ; at the end off your statement. It should be something like that:

public class twoTimes 
{    
    public static void main(String[] args) 
    {       
        for ( int i=1; i<11; i++)
        {
            System.out.println("count is" + i);
        }
    }
}

Upvotes: 0

Mihai Bujanca
Mihai Bujanca

Reputation: 4219

If you add a ; at the end of the for, it looks like the function/command ended. As specified in the previous answers, removing it will ensure your functionality.

public class twoTimes 
    {    
        public static void main(String[] args) 
        {       
            for ( int i=1; i<11; i++)
                System.out.println("count is" + i);
            }
        }
    }

A semicolon (;) always indicates the point where a command would stop. The same thing happens if you create any method and put a ; after, won't work.

Upvotes: 0

Bitco Software
Bitco Software

Reputation: 437

Remove the ; at the end of the for loop:

    for ( int i=1; i<11; i++);  <---------
{
    System.out.println("count is" + i); 
}

Upvotes: 0

kalaero
kalaero

Reputation: 174

Try getting rid of the ( ; ) at the end..

for ( int i=1; i<11; i++);
//-----------------------^^
// Remove that ;

Upvotes: 4

Related Questions