[chuck-users] newbie confusion
Spencer Salazar
ssalazar at princeton.edu
Wed May 10 23:18:48 EDT 2006
the inner loop will never terminate, because 1::second => now
increments now by one second, and then evaluates to now (after the 1
second increase). now is pretty much always positive, and non-zero
numbers evaluate to "true" in conditional expressions, so the inner
loop repeats forever.
The deal with while loops is that they test the conditional
expression once, and then execute ALL of the code between { and }
before they test the conditional expression again. Its perhaps a bit
counterintuitive, but its the standard in typical programming
languages. Consider this loop:
0 => int i;
while( i == 0 )
{
2 => i;
// do some stuff...
0 => i;
}
Even though i is not zero in some parts of the loop, the i is zero
every time the ( i == 0 ) conditional is checked, so the loop repeats
forever.
Your loop is being evaluated like this:
-determine if ( now < later ) is true (it is)
-execute outer while loop expression
-determine if ( 1::second => now ) is true (it is)
-execute inner while loop expression
-determine if ( 1::second => now ) is true (it is)
-execute inner while loop expression
-determine if ( 1::second => now ) is true (it is)
-execute inner while loop expression
... and so on forever ...
So the conditional expression of the outer loop never gets another
chance to be tested.
you can get what you want with this:
while( now < later )
{
1::second => now;
<<< now, "inner loop" >>>;
}
I dont know of any in-depth resources on ChucK programming, but I
think learning programming basics in Java or C would definitely
contribute to a better understanding of some of the parts of ChucK
with which you seem to be less familiar.
Also, be sure to look through all of the example code if you havent
already.
http://chuck.cs.princeton.edu/doc/examples/index.html
hope this helps
spencer
On May 10, 2006, at 9:44 PM, plutek wrote:
> obviously i'm a complete newbie here.... could someone please
> explain to me why the inner loop in this code is not terminated by
> the outer loop, after ten seconds:
>
> now + 10::second => time later;
>
> while( now < later )
> {
> while( 1::second => now )
> {
> <<< now, "inner loop" >>>;
> }
> }
>
>
> i suppose i'm victim to some horribly wrong assumption about How
> Things Work, which brings up my other question:
> is there some material somewhere (more than just the tutorials)
> which would take me deeper into a general understanding of the
> mechanics of coding in ChucK? the language reference is excellent,
> but doesn't help one to learn how to put all the details together
> into coherent code. i do have a decent understanding of the
> principles of synthesis, so it's really questions of logic flow,
> semantics, and style that concern me. i don't know if material
> related to other languages will be applicable, due to the strongly-
> timed nature of ChucK.
>
> any advice? (other than enrolling in princeton) :-)
>
> many thanks in advance!
>
>
> .pltk.
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
More information about the chuck-users
mailing list