Results 1 to 5 of 5
  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default Change my for loop

    Java Code:
    int a = 1; // a gets value from user
    int j=0;
    for(int i=a;i<10;i++)
    {
    // code goes here
    j = j++;
    if(j==10)
    break;
    }
    I have the following piece of code. How can I alter for loop so that I can remove the while condition form my code.

    Please advice.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    By while condition, I assume you mean the i < 10 part of your for loop. What are you trying to accomplish by getting rid of this?

  3. #3
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    I meant can we remove the if condition below and embed this in the for loop?
    Java Code:
    if(j==10)
    break;
    Apologies for the confusion.

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Java Code:
    for(int i=a;i<10 && j != 10;i++)
    It seems like the above code will compile but I'm not sure if it will accomplish what you want. I've never thought about putting a complex condition there.

    If all else fails, what you had in the first place is completely valid.

  5. #5
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Perfect. It worked.
    I wanted to know how to put two conditions in a for loop.

    Thanks mate.

Similar Threads

  1. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 07:44 AM
  2. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  3. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:44 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 04:11 PM

Posting Permissions

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