SquareGame Confusion with while loop

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

SquareGame Confusion with while loop

textbook91
In this sample code from the SquareGame class, the variable 'key' is declared, but not assigned to any value. Later on, a while loop uses the 'key' variable in its expression(key=0). How does the while statement tell what value 'key' is, if 'key' has not been assigned to anything.  
 
   method void run() {
        var char key;
        var boolean exit;

        let exit = false;

        while (~exit) {
            // waits for a key to be pressed.
            while (key = 0) {
                let key = Keyboard.keyPressed();
                do moveSquare();
            }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SquareGame Confusion with while loop

cadet1620
Administrator
This post was updated on .
The Jack language specification says that "var" variables are initialized to 0.
Note, however, that Memory.alloc() does not initialize the memory it returns.

(If you already wrote your VM translator you did this by initializing functions' local segments to 0.)

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SquareGame Confusion with while loop

textbook91
thanks for clearing that up!
Loading...