Array assignment - Jack language

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Array assignment - Jack language

cces4415
Two issues here:
1. Is there a way to assign multiple values to an Array object? Like in C we write
int ary[10] = {0,1000,1000,1000,1013,1,19,3000,1005,0};
because for some reasons I have to build a constant integer array with size over 200.
2. Is it legal to write "else if"? Or do I have to write stacked "if else"(an if else within another)
Eg.
if(a>8){do something();}
else if(a<8){do something();}
else {do something();}

VS
if(a>8){do something();}
else {
   if(a<8){do something();}
   else {do something();}
}

Again, for some reasons I have to build (switch-case-like) conditions in a row.

I'm a university student in Taiwan, doing final project by writing a simple game with jack.
Thanks a million.
Deemo Harlos
Reply | Threaded
Open this post in threaded view
|

Re: Array assignment - Jack language

cadet1620
Administrator
cces4415 wrote
1. Is there a way to assign multiple values to an Array object? Like in C we write
int ary[10] = {0,1000,1000,1000,1013,1,19,3000,1005,0};
There is no way to assign multiple values in a single statement. The only assignment statement available in Jack is the "let" statement that assigns a single value.
because for some reasons I have to build a constant integer array with size over 200.
2. Is it legal to write "else if"? Or do I have to write stacked "if else"(an if else within another)
All "if" and "else" statements must have braces. When I've needed to write switch-like structures with several cases I've put all the closing braces on the final line:
if (x=1) {
    do one();
} else { if (x=2) {
    do two();
} else { if (x=3) {
    do three();
} else { 
    do default();
}}}
The Jack language was designed to be very easy to parse and compile since students are going to write a Jack compiler as part of the course. This simple syntax and the limited statement types does, however, make it cumbersome to use for complex programs.

--Mark