int a <- 5*3;
int a <- 5^2;
int a <- int(5/3); //取整
int a <- round(5/2); //四舍五入
int a <- 5 mod 3; //取余
float a <- sin(90); //sin函数
int a <- rnd(100); //随机数
逻辑运算
GAML逻辑运算符主要有 and、or 以及 !(表示not),! 在使用时应该放在表达式的前面。
bool a <- true or false;
bool b <- true and false;
bool c <- !true;
int index <- rnd(100);
if (index <10){
write 'index is smaller than 10';
}
else if (index>=10 and index<80){
write 'index is bigger than 10 but smaller than 80';
}
else {
write 'index is bigger than 80';
}
GAML还提供一种使用?来简化表达的条件语句。
string a <- (1>3) ? "this is true" : "this is false";
//此句等价于
if (1>3){
string a <- "this is true";
}
else{
string a <- "this is false";
}
循环语句
GAML使用关键字loop来实现循环语句。
loop times: 2 { write 'hello world';} //循环两次
loop while: true{write 'hello world';} //无限循环
loop i from: 0 to: 5 step:2 {write i;} //输出0-2-4
loop i over: [0,2,5]{write i;} //输出0-2-5