Results 1 to 17 of 17
- 10-06-2010, 05:32 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-06-2010, 05:34 PM #2
- 10-06-2010, 05:53 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
Do you mean move back to first action in the loop ? in that case do this
else
count = 1;
}
- 10-06-2010, 05:59 PM #4
- 10-06-2010, 06:31 PM #5
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.Java Code:while(count<12) { /*line1*/ a=a+1; if(condition1) staement; else move back to line 1; }
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!
- 10-07-2010, 02:41 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-07-2010, 02:47 PM #7
- 10-07-2010, 02:49 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-07-2010, 02:52 PM #9
And what about just using a while loop? I think a do-while loop would work pretty nicely here.
- 10-07-2010, 02:53 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-07-2010, 03:00 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-07-2010, 03:03 PM #12
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
- 10-07-2010, 03:15 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-07-2010, 03:24 PM #14
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.
- 10-07-2010, 04:08 PM #15
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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
- 10-07-2010, 04:12 PM #16
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?
- 10-07-2010, 04:20 PM #17
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Java applet won't run since moving to Win 7 64 bit.
By bilzmale in forum New To JavaReplies: 3Last Post: 08-01-2010, 11:17 AM -
Moving files in Java
By techbossmb in forum Advanced JavaReplies: 1Last Post: 10-09-2009, 01:55 PM -
Need Help with Java 2d - moving train
By rtm09 in forum New To JavaReplies: 7Last Post: 04-15-2009, 12:28 AM -
inputting and writing a file backwards
By jigglywiggly in forum New To JavaReplies: 0Last Post: 03-18-2009, 07:24 PM -
Really need help with an assignment... counter control loop...
By maxpower1000sa in forum New To JavaReplies: 7Last Post: 02-21-2009, 10:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks