Results 1 to 17 of 17
  1. #1
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default Moving control backwards within loop in java

    Hi all,
    Can anyone suggest me that how to transfer the control backwards to a particular staement within the loop after some condition is satisfied. For ex-:
    while(count<12)
    {
    /*line1*/ a=a+1;
    if(condition1)
    staement;
    else
    move back to line 1;
    }
    any response will be highly appreciated
    Regards
    Rishabh

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

  3. #3
    tom99 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Do you mean move back to first action in the loop ? in that case do this

    else
    count = 1;
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    Quote Originally Posted by tom99 View Post
    Do you mean move back to first action in the loop ? in that case do this

    else
    count = 1;
    }
    I don't think that will quite do it. What if there are statements after the else that he wants to skip over? Granted, the OP is probably just approaching this wrong: a simple while loop would probably also do the trick.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Java Code:
    while(count<12)
    {
      /*line1*/ a=a+1;
      if(condition1)
        staement;
      else
        move back to line 1;
    }
    This code you provided actually isn't far off of what you're looking for, assuming I understand correctly. Until count>=12, the loop will continue to execute in that order. Hence, unless "condition" is satisfied, it will migrate back to the start of the loop again.

    Here, try executing this code:
    Java Code:
    int a = 5;
    while (a < 10) {
        System.out.println("The code has reached line 1 again."); // Line 1
        if (a == 7) // Condition1
            System.out.println("a == 7"); // Statement
    }

    ...then you should see how your loop is working.

    Best of luck!

  6. #6
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Hi Zack,
    Thats what my question is, that how to move the control back to line1 after the condition1 falters and execute all the statements from line1 again....
    Regards
    Rishabh Jha

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    Quote Originally Posted by jharishabh7 View Post
    Hi Zack,
    Thats what my question is, that how to move the control back to line1 after the condition1 falters and execute all the statements from line1 again....
    Regards
    Rishabh Jha
    And what happened when you tried the other suggestions?

  8. #8
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Hi Kevin
    Thanx for providing the link, but that really doesnt help, as the unlabelled break staement moves you out of the current loop while the unlabelled continue statement skips all the subsequent lines within the loop and starts the next loop, I dont want to move out of the loop, I just want to move the control back to a particular statement within the same loop. I also tried using labelled break and continue, but acoording to the rules the label should also be present along the loop, not inside it.
    Regards,
    Rishabh Jha

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    And what about just using a while loop? I think a do-while loop would work pretty nicely here.

  10. #10
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Kevin,
    Other suggestion also havnt helped to that extent, Ok let me make the question a bit simpler, You all know that with goto statement in c or c++ u can move the control forwards or backwards in a program, and you must also be knowing that goto statement cant be used in JAVA, so what is the substitute of that goto statement to move the control to a statement backwards in JAVA
    Regards
    Rishabh Jha

  11. #11
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Kevin
    Let me see if do while accomplishes the task or not, Though i have solved my problem, but not by using this moving backwards concept. I just want to implement by considering it as a challange. And do u have any good project in mind which can be implemented in JAVA which I can also consider as my minor project.
    Regards
    Rishabh Jha

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    I guarantee a do-while can solve the problem. How did you solve it?

    And I'd recommend just programming things you're interested in. Or you could check this out: Project Euler

  13. #13
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Kevin,
    I took a look at the following and I think it will help. By the way I solved my original problem using some different algorithmnot by the one I mentioned here. Blocks and Statements

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    Quote Originally Posted by jharishabh7 View Post
    Kevin,
    I took a look at the following and I think it will help. By the way I solved my original problem using some different algorithmnot by the one I mentioned here. Blocks and Statements
    Okay, that is certainly one way to do it. However, trying to find a work-around in order to simulate a go-to statement is probably not the best solution. Java took away go-to statements for a reason, and if you find yourself in need of one, it's almost definitely a sign that you should rethink your design.

  15. #15
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Kevin,
    Sorry to say, the link which I provided also didnt helped as what I was trying to do was
    while(count<12)
    {
    label1: a=a+1;/*line1*/
    if(condition1)
    staement;
    else
    continue label;
    }
    I thought this would do the trick as continue statement will bring back the control to target label. But it is written that the continue target must be a while, do, or for statement or a compile-time error occurs. I think there seems to be some defect in the goal I am trying to achieve. But thanks for the support. However in future if you find a solution to this than please inform me.
    Regards Rishabh Jha

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    Yeah, like I said, trying to find a work-around to mimic a go-to probably isn't the best route.

    Hint: You want to do line1 while condition1 is not true, right?

  17. #17
    jharishabh7 is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    I think that would probably do the work. Though I have dveloped another algorithm to solve my original problem but I think this would accomplish previous one also, thanks once again.
    Regards,
    Rishabh Jha

Similar Threads

  1. Java applet won't run since moving to Win 7 64 bit.
    By bilzmale in forum New To Java
    Replies: 3
    Last Post: 08-01-2010, 12:17 PM
  2. Moving files in Java
    By techbossmb in forum Advanced Java
    Replies: 1
    Last Post: 10-09-2009, 02:55 PM
  3. Need Help with Java 2d - moving train
    By rtm09 in forum New To Java
    Replies: 7
    Last Post: 04-15-2009, 01:28 AM
  4. inputting and writing a file backwards
    By jigglywiggly in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 08:24 PM
  5. Replies: 7
    Last Post: 02-21-2009, 11:52 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
  •