Computer Science Canada Ending A Loop From Procedure |
Author: | Warsick [ Mon Feb 08, 2010 1:06 pm ] | ||
Post subject: | Ending A Loop From Procedure | ||
End A Loop From A Procedure I'm trying to call a procedure that will end a loop without having to implement the entire code into my loop. 'Exit' Wont Work I've tried to put exit in the procedure, it said that it is only available if it is within a loop or for statement. I do understand the problem but I'm trying to find a work around. Attempted To Insert A Process I tried to use a processes rather then a procedure in a blind attempt to fork around it but that also failed. Here is the code:
I Am Using Turing 4.1.1 Also the last time I actually worked on Turing was about four years ago, so I'm a bit rusty on the commands. |
Author: | chrisbrown [ Mon Feb 08, 2010 1:39 pm ] | ||||
Post subject: | Re: Ending A Loop From Procedure | ||||
I assume you are trying to do something like this:
Depending on where/how you use p, you could try something like:
|
Author: | USEC_OFFICER [ Mon Feb 08, 2010 8:25 pm ] |
Post subject: | RE:Ending A Loop From Procedure |
I believe that you can't do that. The procedure may not always be in a loop the way you have it, which would explain the error. Having a procedure for only three lines of code is a bit of a waste though. |
Author: | TerranceN [ Mon Feb 08, 2010 9:54 pm ] | ||||
Post subject: | Re: Ending A Loop From Procedure | ||||
There are two different ways you could do this AFAIK: One, like what methodoxx posted, you could return a boolean indicating whether or not to exit the loop. Working example:
The other would be to use a global boolean variable to indicate whether to continue looping, then in the procedure, toggle the variable. Working Example:
Hope this helps. |
Author: | copthesaint [ Mon Feb 08, 2010 10:31 pm ] | ||
Post subject: | Re: Ending A Loop From Procedure | ||
Here a better one for you, and I bet fits with your knowledge of turing.
Now really this method of completing the task is basic, but If all you want to do is exit the program then this is perfect for you, dont worry about declairing the variable over and over again, the real thing that slows turings speed is functions, procedures (but not as bad as functions), and anything that involves GUI, and well Accually EVERYTHING, but declairing variables and seting primative values. Lol... |
Author: | DemonWasp [ Tue Feb 09, 2010 1:39 am ] |
Post subject: | RE:Ending A Loop From Procedure |
@copthesaint: I can think of nothing to indicate that a procedure taking no values is any faster than a function. Possibly the rare exception if you're passing several dozen parameters, but the difference should only be noticable if you're calling it millions of times a second. Worse, your version won't allow the program to do any cleanup / finalization tasks, like terminating network connections, closing open file streams, freeing memory (although Turing handles this for you) and the like. |
Author: | copthesaint [ Tue Feb 09, 2010 2:12 am ] |
Post subject: | RE:Ending A Loop From Procedure |
Hey, I said it was ez, not good lol... I just put it because it because I dont know how much experience this user has with programing...as for leaks, never bothered in turing lol., Your right I read back what I wrote for procedures and functions lol. |
Author: | USEC_OFFICER [ Tue Feb 09, 2010 12:54 pm ] |
Post subject: | RE:Ending A Loop From Procedure |
Wait, how does putting a procedure that does nothing in a loop end the game? |
Author: | Euphoracle [ Tue Feb 09, 2010 2:41 pm ] |
Post subject: | RE:Ending A Loop From Procedure |
He used `quit` to terminate, which is not the right way to do this. |
Author: | andrew. [ Tue Feb 09, 2010 3:10 pm ] |
Post subject: | Re: RE:Ending A Loop From Procedure |
copthesaint @ Tue Feb 09, 2010 2:12 am wrote: Hey, I said it was ez, not good lol... I just put it because it because I dont know how much experience this user has with programing...as for leaks, never bothered in turing lol., Your right I read back what I wrote for procedures and functions lol. If he/she doesn't have much experience, then why are you trying to teach them the incorrect way? Teach them the proper way so that learn, otherwise they will not become better and more experienced with this kind of stuff. Also, what was the point of putting everything in a procedure and calling it from a loop? You may as well just copy and paste the stuff into the loop because the procedure does nothing; It doesn't even organize the code a bit. |