This is the 12th day of my participation in the August Text Challenge.More challenges in August
Statement Statement and value
The value of the conditional statement
In AviatorScript, each statement has a value, not just the entire script (see Section 3.6 multi-line expressions and returns). For example, if conditional statements also have a value, which is the result of the actual branch statement:
## examples/statement_value.av
let a = if (true) {
1
};
p("a is :" + type(a) +", " + a);
Copy the code
Output:
a is :long, 1
Copy the code
Because the condition is true, we execute the branch statement inside the braces, and as a rule, a continuous multi-line expression returns the value of the last expression, which is 1, the result of the whole branch statement is 1, so the result of the whole if statement is also 1, and we assign it to A and print out the type and value. Notice the semicolon at the end of the if statement; It is used to end an assignment statement by assigning the result of the conditional expression to a. If not, an error will be reported:
Exception in thread "main" com.googlecode.aviator.exception.ExpressionSyntaxErrorException: Syntax error: missing '; ' for let statement at 92, lineNumber: 8, token : null, while parsing expression: ` ## examples/statement_value.av let a = if (true) { 1 } p("a is :" + type(a) +", " + a); ^^^ ` at com.googlecode.aviator.AviatorEvaluatorInstance.innerCompile(AviatorEvaluatorInstance.java:1293) at com.googlecode.aviator.AviatorEvaluatorInstance.compile(AviatorEvaluatorInstance.java:1256)Copy the code
What if we decide that the condition should be false? :
let a = if (false) {
1
};
p("a is :" + type(a) +", " + a);
Copy the code
Execute output:
a is :nil, null
Copy the code
Because there’s no else branch, the default is nil, and the result of the entire if condition is nil.
Let’s try adding the else branch:
let a = if (false) {
1
} else {
2
};
p("a is :" + type(a) +", " + a);
Copy the code
Now the result is 2:
a is :long, 2
Copy the code
The value of the loop statement
In fact, not only conditional statements, but for/while loops also have values:
let b = for x in range(0, 10) {
x
};
p("b is :" + type(b) +", " + b);
Copy the code
The result of the loop statement is the value returned during the last iteration, so here is the value of the last iteration x, which is 9:
b is :long, 9
Copy the code
What if I add break/continue? :
let b = for x in range(0, 10) { if x == 2 { break; }}; p("b is :" + type(b) +", " + b);Copy the code
Because when the last iteration executes break or continue, the result will be nil:
b is :nil, null
Copy the code
But what about return? Otherwise, if the last iteration returns a return, then the result of the entire loop statement is the result of return, just as we did in Section 3.6. More generally, if the entire statement returns the result of a return and is assigned to a variable, then the return will only return from the statement, not from the entire script:
let b = for x in range(0, 10) { if x == 2 { return x; }}; p("b is :" + type(b) +", " + b);Copy the code
We call return 2 inside the loop; , but instead of returning from the entire script (otherwise the following print would not be executed), it simply returns from the loop statement and assigns the result to B:
b is :long, 2
Copy the code
Value of a Block
A block enclosed in braces also has a value, which is the value of the last expression executed in the block:
## Block's value
let c = {
let a = 1;
let b = 2;
a + b
};
p("c is :" + type(c) +", " + c);
Copy the code
Execute output:
c is :long, 3
Copy the code
The same goes for return. If a block is assigned to a variable, it returns from the block:
let c = { let a = 1; let b = 2; if a > b { return a; } else { return b; }}; p("c is :" + type(c) +", " + c);Copy the code
The result will be the value of the local variable B in the block:
c is :long, 2
Copy the code