electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
Using break to exit a for() loop when recalling
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [4 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Wed Aug 10, 2011 4:06 am    Post subject: Using break to exit a for() loop when recalling Reply with quote  Mark this post and the followings unread

Assumed we have a function with a for() statement. We call this function again, but the for() statement hasn't finished yet.

How can we break; (restart) the for() statement?

f.e.

fun void exe(){

for(0=>int i; i<32; i++){
<<<i>>>;
200::ms=>now;

}

}

while(1){

//we call exe() every 2 sec, but the for() statement hasn't finished yet
spork~exe();
2000::ms=>now;

}


Thanks in advance!
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 371
G2 patch files: 100

PostPosted: Wed Aug 10, 2011 5:03 am    Post subject: Reply with quote  Mark this post and the followings unread

Tip: select the code and click the "code" button when editing the post, to make it more readable. Also disable HTML and smilies (maybe you did this).

Here's one possible solution. It may not work like you want if you start waiting, say, 170 ms in the exe loop and 2100 ms in the main loop.


Code:
0 => int exeID;

fun void exe(){
        ++exeID => int myID;
        for(0=>int i; exeID == myID && i<32; i++){
                <<<i>>>;
                200::ms=>now;

        }

}

while(1){
        //we call exe() every 2 sec, but the for() statement hasn't finished yet
 
        spork~exe();
        2000::ms=>now;

}


If you always want to interrupt the small loop even if it's hanging on the wait (maybe you have some code after "200::ms=>now;" that you want to execute immediately after breaking), you can introduce an Event instance, and an extra shred method for signalling that event after a set time. Then you wait for that event in your loop instead of the literal time. Then change the code at the start of exe to 1) increment exeID like before 2) signal the event. 3) start the loop like before.

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Wed Aug 10, 2011 5:42 am    Post subject: Reply with quote  Mark this post and the followings unread

Stefan you are gorgeous.

thanks a lot, very clear!

Cheers
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 371
G2 patch files: 100

PostPosted: Wed Aug 10, 2011 6:11 am    Post subject: Reply with quote  Mark this post and the followings unread

Embarassed Very Happy
_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [4 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use