Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Looping procedures??
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
pttptppt




PostPosted: Mon Feb 20, 2017 3:23 pm   Post subject: Looping procedures??

What is it you are trying to achieve?
Im making a game. When an event happens (searches for event in a loop), it exits from the loop and a procedure gets called using a button. After this procedure occurs, I WANT the program to return to the beginning of that loop to search for the event.

Explanation of why in case interested: Im making a board game. When 2 characters want to battle, the user does so with the click of a button and the battle gets triggered. After, the loser of the battle SHOULD be removed from the board and the victor SHOULD stay.


What is the problem you are having?
After the battle is triggered, I can't find a way to return to the main program (the board game with its movements). The only way i could think of is using a GOTO line but that's not available. Also, apparently you can't loop procedures


Describe what you have tried to solve this problem
I have tried numerous things including moving around procedures and whatnot but still to not avail


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<Add your code here>



Please specify what version of Turing you are using
<Answer Here>
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Feb 22, 2017 11:04 am   Post subject: RE:Looping procedures??

Remember that when a function finishes executing, it returns control to the code that called it. So you don't really need to 'go back'. Just let the function finish!

For example:

code:

loop
    %Main board game with movements here
   
    if (battle is triggered) then
        doBattle() %This function handles the entire battle sequence
    end if
end loop


When doBattle() finishes, the very next lines are 'end if' and 'end loop', which means it goes back to the top of the loop and starts over. We never actually told it to, that's just the normal flow of execution.

Your doBattle() function would look sort of like this:

loop
%battle logic
exit when enemyHP <= 0
end loop

When the enemy dies, the function terminates. When the function terminates, it returns control to the loop, which restarts, and we're back to the main game.

iirc, the keyword 'continue' is basically the same as 'GOTO the top of the loop'. You can plug that in anywhere to restart the loop, but it's generally not necessary and you'll learn better habits following the example above.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: