Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with Loop and int count

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    Mexico
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Loop and int count

    I'm fairly new to programming, just started yesterday with a book I downloaded of the net. The book comes with a couple of examples on how to code and they all seem to work fine except for this loop.

    class fordemo {
      public static void main(String args[] ) {
       int count;
     
       for(count = 0;  count < 5; count = count+1);
         System.out.println(" This is count: " + count); 
     
     
       System.out.println("Your are all done! "); 
     
        }
    }

    I can't seem to find the problem. All it gives me is,
    "This is count: 5
    You are all done!"

    When it's supposed to give me,
    "This is count: 1
    This is count: 2
    This is count: 3
    This is count: 4
    You are all done!"



    Edit: Nevermind! Found the error, semicolon after the for loop is not necessary. Should be...

    class fordemo {
      public static void main(String args[] ) {
       int count;
     
       for(count = 0;  count < 5; count = count+1)
         System.out.println(" This is count: " + count); 
     
     
       System.out.println("Your are all done! "); 
     
        }
    }
    Last edited by v6277; June 15th, 2014 at 02:32 PM. Reason: Solved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Loop and int count

    Statements are ended by the ; character. The for statement ends at the ;
    the next statement following the for is not part of the for.

    NOTE: The statements in an for statement (and other loops also) should be enclosed with {}s

    Also why is count defined outside of the for loop? The compiler was giving you an error message that you ignored.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Help with Loop and int count

    Quote Originally Posted by Norm View Post
    [...]
    Also why is count defined outside of the for loop? The compiler was giving you an error message that you ignored.
    Why would that be an error?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Loop and int count

    Why would that be an error?
    Wait until you have written another 10000 lines of code. I'm sure you will get one eventually.
    Try this:
          for(int count=0; count<2; count++);
          System.out.println("count="+count);  // error: cannot find symbol

    I was assuming the OP was getting this error, and changed the code by defining count outside the for statement so the println could find it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Location
    Mexico
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Loop and int count

    The loop would not take place if the statement ended at the for, I had to include the println.

    This is just learning how to do this stuff, the next paragraph actually introduced blocks. I'm just taking one step at a time to make sure I can learn correctly.

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Location
    Sandwich
    Posts
    14
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Help with Loop and int count

    One peice of advice it won't cause an error or anything its just faster. You can replace count=count+1 with count++.

Similar Threads

  1. Using a Loop and Nested Ifs to Count Number of Vowels in a String
    By Potat in forum Loops & Control Statements
    Replies: 17
    Last Post: February 21st, 2013, 09:35 PM
  2. stuck trying to count sum of even and odd numbers for reading loop.
    By gio in forum Loops & Control Statements
    Replies: 2
    Last Post: February 21st, 2013, 11:23 AM
  3. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 01:31 AM
  4. Replies: 2
    Last Post: October 5th, 2012, 01:14 PM
  5. Do While loop + switch, Vowel Count
    By mwardjava92 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 9th, 2011, 11:46 AM

Tags for this Thread