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 5 of 5

Thread: What is the difference between the belows code

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is the difference between the belows code

    for(int i=0;i<newarray.length;i++)
    {    	
     newarray[i]=cards[i];
     cards=newarray;
    }


     for(int i=0;i<newarray.length;i++)
    {	    	
     newarray[i]=cards[i];
     cards=newarray;
    }
    including parenthises


    why with the second statement the the variable i goes only one time since in the first statement goes 51 times which is newarray.length equal to 51.

    Please help me enyone to understand this.
    Last edited by lulzimfazlija; January 21st, 2011 at 07:11 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What is the difference between the belows code

    Ok, well there isnt enough information for me to do a run of my own, but I suspect your assumption is incorrect.

    The simple truth is that as long as newarray is 51 indexes long, both loops will run 51 times. The question that you need to ask is: why doesn't it appear that the first runs 51 times?

    Which brings me to the question:
    Do you know the difference between
    int x=0;
    if(x==0)
    System.out.println("Output1");
    System.out.println("Output2");
    and
    int x=0;
    if(x==0)
    {
    System.out.println("Output1");
    System.out.println("Output2");
    }

    The answer has to do with statement blocking. Between any two curly brackets is what is called block code. Whenever a condition is met, a loop is running, a method is called, ect. everything in that block of code (with the exception of code that is run on conditions or if exceptions are thrown) will run. All code after the block obviously runs after the block. Now, if no curly brackets are added to indicate a block, only the first line of code immediately after the condition is called or only the first line of code immediately after the loop declaration runs while the loop runs.

    So, in your above code:
    For the first snippet:
    Only newarray[i]=cards[i]; is ran in the loop. cards=newarray; is consider "outside the scope of the loop" and thus does not run when the loop runs. #1 above is the same as this:
    for(int i=0;i<newarray.length;i++)
    {
    newarray[i]=cards[i];
    }
    cards=newarray;

    For the second snippet:
    Both newarray[i]=cards[i]; and cards=newarray; runs while the loop runs.


    Tell me if that makes sense. If you already know all of this, add more code so we can see the context of your problem. Otherwise, we have no real way to determine what is happening.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is the difference between the belows code

    I'm not sure what your code is supposed to do, as the logic seems pretty wonky.

    But since your question was on syntax, I'll just answer that part:

    Curly brackets denote a block of code, which can be used anywhere a single statement is allowed (source: Expressions, Statements, and Blocks (The Java™ Tutorials > Learning the Java Language > Language Basics))

    Basically, in your first code snippet (where are your code tags?), only the newarray[i] = cards[i] line is happening "inside" the loop. In your second code snippet, both lines are happening "inside" the loop because both lines are inside the curly braces.

    Does that answer your question? I'm not sure what you're talking about with "the variable i goes only one time".

    Edit- Too slow. What aussiemcgr said.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What is the difference between the belows code

    Quote Originally Posted by KevinWorkman View Post
    Edit- Too slow. What aussiemcgr said.
    pwn'd
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What is the difference between the belows code

    I don't know the type of arrays the OP is using, but it might be better to set the two arrays to be equal after the for loop, rather than doing it every time you add something.

Similar Threads

  1. [SOLVED] Difference between == and equals.
    By goldest in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2010, 03:15 PM
  2. Replies: 1
    Last Post: August 13th, 2010, 06:58 AM
  3. [SOLVED] difference between fileReader and bufferReader
    By shadihrr in forum Java Theory & Questions
    Replies: 6
    Last Post: June 8th, 2010, 01:26 AM
  4. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 06:24 AM
  5. Difference between Vector and Arraylist
    By Lil_Aziz1 in forum Collections and Generics
    Replies: 4
    Last Post: January 3rd, 2010, 11:03 AM