Results 1 to 4 of 4
  1. #1
    Arcturus is offline Member
    Join Date
    Sep 2015
    Posts
    4
    Rep Power
    0

    Default What is the difference between these 2 scripts? (they produce different results)

    After reading through this beginners java book, while going through it I'd written this counting script out by pretty much copying it down (but typing it myself) and changing minor things, so when I finished the book, I decided to try and write the same script but just off of what I'd learned.

    Here's the script copied directly from the guide;

    Java Code:
    /*
      Demonstrate the for loop.
      Call this file ForDemo.java.
    */
    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("Done!");
      }
    }

    And here's the one written purely by what I'd learned so far (not as pleasing to the eye but none the less, roughly the same thing);

    Java Code:
    class Counter {
    public static void main(String args[]) {
    
    int counter;
    
    for(counter = 0; counter < 5; counter = counter+1);
    System.out.println("counter is equal to " + counter);
    
    }
    }


    By the way, just thougt I'd say, the script from the guide does what it's supposed do, says "This is count: x", repeats 5 times, then stops.

    Mine clearly has some issues, because it just says "counter is equal to 5", that's the only line of output.

    edit: after looking at the script straight after posting, i realised i had a semi colon after counter+1) - and the guide script does not, I'm gonna remove that and see what happens..

  2. #2
    Arcturus is offline Member
    Join Date
    Sep 2015
    Posts
    4
    Rep Power
    0

    Default Re: What is the difference between these 2 scripts? (they produce different results)

    Well what d'ya know, that fixed it, can anyone explain why the semi colon made a significant difference, is it because with the semi colon the statement is terminated, and so doesn't loop?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: What is the difference between these 2 scripts? (they produce different results)

    Does the beginners book really show the definition for the count variable outside of the for statement?
    That is not normally where the control variable is defined. It is often a beginner's method to overcome an undefined variable problem caused by the ; that you had or by missing {}s

    The normal way:
    Java Code:
      for(int count=0; count<max; count++) {
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: What is the difference between these 2 scripts? (they produce different results)

    Quote Originally Posted by Arcturus View Post
    Well what d'ya know, that fixed it, can anyone explain why the semi colon made a significant difference, is it because with the semi colon the statement is terminated, and so doesn't loop?
    It loops, there is just nothing in the loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. One of my first scripts
    By walia6 in forum New To Java
    Replies: 2
    Last Post: 07-18-2015, 01:53 PM
  2. How do I seperate names in this scripts
    By techboy992 in forum New To Java
    Replies: 4
    Last Post: 02-26-2014, 09:29 AM
  3. Scripts
    By AndrewM16921 in forum Advanced Java
    Replies: 1
    Last Post: 02-07-2013, 06:19 PM
  4. Ant: Using Ant target to generate shell / batch scripts
    By ignisfatuus in forum Advanced Java
    Replies: 0
    Last Post: 07-22-2011, 09:21 PM
  5. Run ant scripts using ProcessBuilder
    By prsindagi in forum Advanced Java
    Replies: 6
    Last Post: 02-11-2009, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •